Click here to Skip to main content
15,889,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what are the differences between constructor that compiler will create for first class and constructor in second Test class definitions?


C++
class Test
{
  // no default constructor(compiler will create a default constructor)
};

and
C++
class Test {
public :
Test() {} // default constructor

};
Posted
Comments
enhzflep 29-Jul-12 6:15am    
There are none that I'm aware of.
nv3 29-Jul-12 8:25am    
That's a valid solution of the question.
enhzflep 29-Jul-12 8:32am    
I considered adding it as a solution, but felt that my answer of "no", which I padded to 7 words was simply too short. I also felt that a comment would leave the Q with 0 solutions and invite further discussion.
Thank-you for your comment. :)

You can check by creating two classes, one with each type of compiler, and generating assembly code (somewhere in your project settings, depends on which compiler and IDE you are using).
If you find any differences, please publish a tip!

Best wishes,
Pablo.
 
Share this answer
 
Comments
Reza Oruji 29-Jul-12 12:34pm    
thanks,good idea
can you tell how to do that in one IDE?
Pablo Aliskevicius 30-Jul-12 1:51am    
vs2008: right-click the project in 'solution explorer', navigate the tree to [configuration properties],[c/c++],[output files], and from the 'Assembler Output' combo choose 'Assembly with source code'.
Reza Oruji 30-Jul-12 2:47am    
thank you,then how to see code?
Pablo Aliskevicius 30-Jul-12 2:48am    
A file with the .asm extension is generated.
Actually THERE IS a difference between the two cases! This difference has meanin rarely, usually in case of structs, but a class is the same as a struct and the only difference is that the declarations inside the body of a struct start with public while they start as private inside a class.
The difference is that your class without virtual functions and an explicitly defined constructor/destructor is a POD[^]!

Sometimes you can use only PODs, for example:
C++
class CC
{
public:
    //CC() {}
    //~CC() {}
    //virtual void f() {}
    char c;
};

struct SS
{
    int i;
};

union UU
{
    CC c;
    SS s;
};

Uncomment something in CC and the code no longer compiles because you can put only POD types in a union!

EDIT: bugfix
EDIT: Just found out that this solution doesn't exactly the answer to the question, but points out a difference, maybe the only one (who knows it in case of C++... :D).
 
Share this answer
 
v3
Comments
nv3 29-Jul-12 9:12am    
Your answer caught me by surprise. There is indeed a difference between a class with a default constructor and a class with a user-defined constructor that is empty! Got my 5.
pasztorpisti 29-Jul-12 9:25am    
Thank you! Then I may have another surprise for you:

struct POD
{
int i;
};

class Owner
{
public:
Owner()
: m_InitializedPod()
, m_InitializedPrimitivePod()
{}
private:
POD m_InitializedPod;
POD m_UninitializedPod;
int m_InitializedPrimitivePod;
int m_UninitializedPrimitivePod;
};
«_Superman_» 29-Jul-12 10:00am    
Additional Info - The rule of union members not allowed to have explicitly defined constructors has changed in the latest C++ standard.
pasztorpisti 29-Jul-12 10:08am    
This is also news for me, although Iam against using unions in general. :)
Reza Oruji 29-Jul-12 12:32pm    
thanks

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