Click here to Skip to main content
15,891,744 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
For the following C++ code :

C++
class X{
    public :
    X() {cout<<"constr" ;}
} ;

main () {
    X obj() ;
}


What happens during 'X obj()'. It does not invoke the default argument-less constructor. In fact this statement does not create an object at all because after this statement you can invoke any X member on obj. But neither does the statement give a compilation error.
Posted
Updated 29-Jan-14 4:15am
v2
Comments
phil.o 29-Jan-14 10:24am    
Please do not change the content of your original question after it has been answered; it can make the answer completely useless, if not false.
Richard MacCutchan 29-Jan-14 13:05pm    
Compiling the above produces
visual studio\projects.c++\cpptest\cpptest\cpptest.cpp(71): warning C4930: 'X obj(void)': prototyped function not called (was a variable definition intended?).

I fully agree that while creating a class instance via a default constructor, we do not use parenthesis, i.i., we create an instance of class X as follows :

C++
X  obj ;


But what I am confused about is this : what happens when we give the following

C++
X obj() ;


This statement does not give a compile-time error.
This statement does not call the default constructor of the class for obj either.
After this statement, a class member can not be accessed on obj - compiler says that request for member in ‘obj’, which is of non-class type ‘X()’.

So what is actually happening at this statement if the object is not being created.
 
Share this answer
 
Comments
Aescleal 31-Jan-14 14:09pm    
It's not a compile time error 'cause what you've written is valid C++, it just doesn't do what you thought it should.

You're declaring a function - you're saying to the compiler "Somewhere there's a function called obj which takes no parameters and returns an X. Don't worry if I call it, the linker will sort it out."
Member 10544034 1-Feb-14 3:51am    
Yes thank you so much. I was somehow missing out this simple thing.
You are defining a function prototype in main, that is why no object is being created:

C#
main () 
{
    X obj() ;
}


To correct this, use this syntax to create your object:

C#
main () 
{
    X obj;
}


This is one of the oddities that appears in C++ parsing. If your constructor had parameters, then the parenthesis would be appropriate to place around your construction call. Don't use parenthesis for a default constructed object.
 
Share this answer
 
This class has an automatically generated default constructor because you haven't provided any other constructors. What the automatically generated constructor does is the following: It just calls the default constructor of the base classes and the non-pod data members of the class. Since this class doesn't have any base classes or any non-pod data members the auto-generated default constructor does nothing. When compiled with optimization this constructor call is most probably optimized away.
 
Share this answer
 
Comments
Pete O'Hanlon 29-Jan-14 9:53am    
Well, that was the answer I was going to give. Great job. +5.
pasztorpisti 29-Jan-14 10:07am    
Thank you!
Aescleal 31-Jan-14 14:12pm    
Unless the questioner has edited the question (I didn't check) there is a default constructor. The problem is he/she/it's not creating an object in main, they're declaring a function.
Aescleal 31-Jan-14 14:13pm    
Just seen the tit edited the question to add a default constructor. Why the muppet didn't ask a completely new question is beyond me...

Changed the 1 I gave you to a 5. Sorry about that...
pasztorpisti 31-Jan-14 15:05pm    
No problem, with the "new question" the answer is indeed invalid. :-)

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