为什么在这里没有调用构造函数(Visual Studio C ++ 2013)? [英] Why is there no calling of the constructor here (Visual Studio C++ 2013)?
问题描述
我在visual studio 2013中有一个简短的代码段来测试我正在编写的控件:
I have a short code segment here with visual studio 2013 to test my controls that I am writing:
//void CbmpLoadView::OnRButtonDown(UINT nFlags, CPoint point)
void CbmpLoadView::OnRButtonDown(UINT nFlags, CPoint point)
{
TestCDCDirectControls t1();
int x2;
x2 = 2; // break pont set here
}
//TestCDCDirectControls.h
class TestCDCDirectControls : public CDCDirectControls
{
public:
CDCDirectControls CDC_c1;
TestCDCDirectControls(); //declare the initializer
inline ~TestCDCDirectControls(){ return; }; // default destructor just return
};
和
and
//TestCDCDirectControls.cpp
#include "stdafx.h"
#include "CDCDirectControls.h"
#include "TestCDCDirectControls.h"
/*This class is used to test the CDCDirectControls to make sure that they are working. When an instance is created, it tests the CDC Direct Controls */
TestCDCDirectControls::TestCDCDirectControls(){
int x;
x = 1; //I have the debug break point set here
return;
};
我在TestCDCDirectControls上面的行中设置了调试点.cpp停在那里。但似乎永远不会调用构造函数,因为当我按下鼠标右键时,它会在调试行停止:
I have the debug point set at the line above in "TestCDCDirectControls.cpp" to stop there. But it seems that the constructor is never called, because when I press the right mouse button, it stops at the debug line:
x2 = 2;
但它永远不会停在调试行:
But it never stops at the debug line:
x=1;
发生了什么,如何让它停在调试行
What is going on, and how can I get it to stop at the debug line
x=1;
?
答案:我调整了这样的代码,一切都按预期工作,在t1内创建一个方法,以便它做了一些事情。我很高兴这个问题相对容易通过。
?
Answer: I adjusted the code like this and everything worked as expected, creating a method inside t1 so that it did something. I am glad that issue was relatively easy to get through.
//void CbmpLoadView::OnRButtonDown(UINT nFlags, CPoint point)
void CbmpLoadView::OnRButtonDown(UINT nFlags, CPoint point)
{
TestCDCDirectControls t1;
int x2;
x2 = 2;
x2 = t1.ReturnX1();
}
特别需要创建方法;如果我将方法ReturnX1注释掉,那么构造函数不再被调用(至少没有达到调试点)!
Creating the method was specifically required; if I commented the method ReturnX1 out, then the constructor was not called again (at least the debug point was not reached)!
推荐答案
因为
Because
TestCDCDirectControls t1();
声明一个名为t1的函数,返回TestCDCDirectControls类型的值。另请参见标准C ++ [ ^ ]。
因为声明了一个函数不会实例化任何东西,也不会执行任何构造函数。
另一方面,在你的第二次试验中
declares a function named "t1" returning a value of type "TestCDCDirectControls". See also Standard C++[^].
And because declaring a function does not instantiate anything, also no constructor will be executed.
On the otherhand in your second Trial
TestCDCDirectControls t1;
确实定义了类型为TestCDCDirectControls的变量t1,因此将执行TestCDCDirectControls的构造函数
所以最后不要再添加x2 = t1.ReturnX1();是解决方案,只需以正确的方式定义变量t1即可解决您的问题。
对不起我的英语,无论如何我希望这会有所帮助。
really defines a variable "t1" of type "TestCDCDirectControls" and therefore constructor of "TestCDCDirectControls" will be executed
So finally not your Addition of "x2 = t1.ReturnX1();" was the solution, simply defining the variable "t1" in a correct way solved your issue.
Sorry for my english, anyway I hope this helps.
这篇关于为什么在这里没有调用构造函数(Visual Studio C ++ 2013)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!