Default class members generated by C++

B

BeautifulMind

Hi,
As per the C++ standard, after the compilation of the following class
"Test" how many / which members would be generated by the compiler.
and actually I am also interested behind the logic doing so by C++ for
this empty class "Test" which is not even utilized in the program.

#include <iostream>
using namespace std;

class Test
{
};

void main()
{
}

Many Thanks
 
D

Daniel T.

BeautifulMind said:
As per the C++ standard, after the compilation of the following class
"Test" how many / which members would be generated by the compiler.
and actually I am also interested behind the logic doing so by C++ for
this empty class "Test" which is not even utilized in the program.

#include <iostream>
using namespace std;

class Test
{
};

void main()
{
}

Many Thanks

I don't think any members are *required* to be generated. The "as if"
rule kicks in and since Test isn't used, the compiler need to nothing
with it.
 
N

Neo

Hi,
As per the C++ standard, after the compilation of the following class
"Test" how many / which members would be generated by the compiler.
and actually I am also interested behind the logic doing so by C++ for
this empty class "Test" which is not even utilized in the program.

#include <iostream>
using namespace std;

class Test
{

};
void main()
{

}Many Thanks

Hi,
For above mentioned code nothing will be generated as you have not
used Test class anywhere.
but if the code is

void main()
{
Test obj;
}

Only one bye of will be added as hidden data member. This is to
differentiate among the other instances of same class. default ctor/
dtor, copy ctor and assignment operator will not get generated coz
there is no any need.

Regards
Vikram S
 
B

BeautifulMind

Hi thanks for the reply,
For above mentioned code nothing will be generated as you have not
used Test class anywhere.

class Test
{
};
void main()
{
}

So this means that given the above code, the compiler will remove the
Test class altogether from the object code (which it should I guess)
becuase it is not unilized anywhere?

Regards,
BeautifulMind
 
I

Ian Collins

BeautifulMind said:
Hi thanks for the reply,




class Test
{
};
void main()
{
}

So this means that given the above code, the compiler will remove the
Test class altogether from the object code (which it should I guess)
becuase it is not unilized anywhere?
unilized? Yes and no, the class is empty, so there isn't anything to
remove.

The compiler will get upset with you for writing "void main()".
 
D

Daniel T.

BeautifulMind said:
Hi thanks for the reply,


class Test
{
};
void main()
{
}

So this means that given the above code, the compiler will remove the
Test class altogether from the object code (which it should I guess)
becuase it is not unilized anywhere?

As an absolute? There is no telling. That's a quality of implementation
issue.

The compiler certainly _can_ remove the class, and I think it is safe to
say that, given the correct options, most _do_ remove the class, but
there is no guarantee that all compilers _will_ remove it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top