积极答复者
MFC中结构体使用的问题

问题
-
在头文件中定义一个结构体
struct M
{
CString str;
};
public:
M *Init();
在cpp文件中:
M *Test::Init()
{
M *pM = Init();
return pM;
}
在按钮函数中调用:
{
in();
out();
}
void test::in()
{
M *pM = Init();
pM->str = “hello”;
}
void test::out()
{
M *pM = init();
CString str = pM->str;
AfxMessageBox(str);
}
这里输出为空对话框, 调用Init()函数返回的不是结构体的指针吗? 为什么弹出的是空对话框??
xiao
答案
-
Hi wh_xiao,
您可以在类中定义结构体,如以下代码
class Test { public: struct M { CString str; int i; }pM; CString in() { pM.str=L"hello"; return pM.str; }; void out() { CString str1; str1=pM.str; AfxMessageBox(str1,0,0); }; };
Test test; test.in(); test.out();
如果您的问题解决了,请把有用的回答标记为答案。
谢谢,
Lucy
运行,当您按按钮时会弹出一个写着hello的对话框。然后再按钮函数中加入以下代码:
Lucy Liu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 wh_xiao 2011年2月24日 8:10
全部回复
-
在头文件中定义一个结构体
struct M
{
CString str;
};
public:
M *Init();
在cpp文件中:
M *Test::Init()
{
M *pM = Init();
return pM;
}
在按钮函数中调用:
{
in();
out();
}
void test::in()
{
M *pM = Init();
pM->str = “hello”;
}
void test::out()
{
M *pM = init();
CString str = pM->str;
AfxMessageBox(str);
}
这里输出为空对话框, 调用Init()函数返回的不是结构体的指针吗? 为什么弹出的是空对话框??
xiao
Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development. -
Hi wh_xiao,
您可以在类中定义结构体,如以下代码
class Test { public: struct M { CString str; int i; }pM; CString in() { pM.str=L"hello"; return pM.str; }; void out() { CString str1; str1=pM.str; AfxMessageBox(str1,0,0); }; };
Test test; test.in(); test.out();
如果您的问题解决了,请把有用的回答标记为答案。
谢谢,
Lucy
运行,当您按按钮时会弹出一个写着hello的对话框。然后再按钮函数中加入以下代码:
Lucy Liu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 wh_xiao 2011年2月24日 8:10