在for循环中,MFC属性页不保留标题 [英] MFC property pages don't keep title when in for loop

查看:77
本文介绍了在for循环中,MFC属性页不保留标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一些CPropertyPages以便在CPropertySheet上进行如下操作:

I am trying to create some CPropertyPages to go on a CPropertySheet as follows:

long numberOfPages;
std::vector<myproppage*> pPages;

for( long i=0; i < numberOfPages; i++ )
{
	pPages[i] = new MyPropPage();
	pPages[i]->m_psp.dwFlags |= PSP_USETITLE;
	CString title0(_T("title0"));
	pPages[i]->m_psp.pszTitle = LPCTSTR(title0);
	pPropertySheet->AddPage(pages[l]);

}



在for循环中,标题正确初始化.当到达循环末尾时,刚刚设置的标题将丢失,并且仅包含垃圾内容.如果我不在循环中,则标题不会被垃圾覆盖.请有人帮忙,因为我失去了生存的意愿:)



In the for loop, the title is initialised correctly. Whne the end of a loop is reached, the title that was just set is lost, and just contains garbage. If I don''t have this in a loop the title is not overwritten with garbage. Please could somebody help with this as I am losing the will to live :)
is there a problem with memory that is initialised in a for loop?

推荐答案

如果您查看该循环语句,则在其中定义了CString,因此它是否存在问题?可能超出范围并在循环后释放.尝试在循环外分配它,或在堆上分配CString(只需确保稍后释放).
If you look at that loop statement, the CString is defined within it so it''s probably going out of scope and getting deallocated after the loop. Try either allocating it outside of the loop or allocate the CString on the heap (just be sure to deallocate later).


std::vector<myproppage*> pPages;




and

pPages[i] = new MyPropPage();


是不一样的东西.


CString title0(_T("title0"));

超越您的for循环.并且您无法从其他位置初始化numberOfPages.而且它不再只是CString了.


are not the same thing.

Put
CString title0(_T("title0"));

above and out of your for loop. And you failed to initialize numberOfPages from else where. And its not just CString anymore.

pPages[i]->m_psp.pszTitle = title0;


这篇关于在for循环中,MFC属性页不保留标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆