Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
3.33/5 (2 votes)
See more:
I have a short code segment here with visual studio 2013 to test my controls that I am writing:

C++
//void CbmpLoadView::OnRButtonDown(UINT nFlags, CPoint point)
void CbmpLoadView::OnRButtonDown(UINT nFlags, CPoint point)
{

	TestCDCDirectControls t1();
	int x2;
	x2 = 2;  // break pont set here
}



C++
//TestCDCDirectControls.h

class TestCDCDirectControls : public CDCDirectControls
	{
	public:
		CDCDirectControls CDC_c1;

		TestCDCDirectControls(); //declare the initializer
		inline ~TestCDCDirectControls(){ return; };  // default destructor just return
	};


and

C#
//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;
};



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;
?


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.

C#
//void CbmpLoadView::OnRButtonDown(UINT nFlags, CPoint point)
void CbmpLoadView::OnRButtonDown(UINT nFlags, CPoint point)
{

	TestCDCDirectControls t1;
	int x2;
	x2 = 2;
	x2 = t1.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)!
Posted
Updated 31-Jan-16 3:39am
v4
Comments
Jochen Arndt 31-Jan-16 9:34am    
The compiler may optimise away your code because it does nothing effectively. You might add some code that does something like changing a member variable or printing the assigned value.

BTW: There is no need to use a return statement with void functions like constructors and destructors when not leaving a conditional path.
[no name] 31-Jan-16 9:51am    
I don't think it was an optimization issue. It is simply
TestCDCDirectControls t1;
vs.
TestCDCDirectControls t1();
Jochen Arndt 31-Jan-16 10:05am    
You may be right. I overlooked the parentheses.
[no name] 31-Jan-16 9:41am    
Nope I think you did not found the answer. The difference is
TestCDCDirectControls t1;
vs
TestCDCDirectControls t1();

Check this, FAQ Is there any difference between List x; and List x();? :
Standard C++[^]

....and try to complile something like this:
TestCDCDirectControls t1();
int x2;
x2 = 2;
x2 = t1.ReturnX1();
...you will "feel" the difference.
StephenJElliott 31-Jan-16 14:06pm    
I explicitly tested with ReturnX1() commented out (and the declaration commented out) versus it in place, and it toggles the debug break-point or not. By the way, the Standard C++ reference states, " List x; // Local object named x (of class List)"; so it appears that "TestCDCDirectControls t1;" is the correct way to create a local instance of the variable.

1 solution

Because
C#
TestCDCDirectControls t1();

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
C#
TestCDCDirectControls 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.
 
Share this answer
 
v2
Comments
Aescleal 31-Jan-16 13:26pm    
+5 - that sort of code was described by Scott Myers as C++'s most vexing parse.
[no name] 1-Feb-16 2:42am    
Thank you
StephenJElliott 31-Jan-16 14:10pm    
The surprising thing is that defining, "TestCDCDirectControls t1;" was not enough to get the compiler to invoke the constructor. A method had to be called in order for the constructor to actually be invoked (or at least for the break-point to stop there). It is kind of strange that the compiler does this; be careful with the constructors to test that they are actually getting invoked, especially in cases of constructor-only classes.
[no name] 1-Feb-16 2:45am    
Thank you for accepting.
But than it is probably the Compiler which recognizes that the instance of t1 is never used and therefore removed it in the sense of "optimizing". To prove this you may find a Settings in the Project/Compiler Options.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900