Reputation: 513
I am working on C++. I am using MVSV 2010.
When I compile a source code and dump the memory layout of all class with -d1reportAllClassLayout.
For example, I declare struct:
struct my_struct{
int a;
};
And the memory layout of struct as following:
class my_struct size(4):
+---
0 | a
+---
Does it mean that C++ compiler consider struct as same as class at everything? (Expept to default access specifier)
If that, then how about the constructor and deconstructor of struct?
Is there a default constructor and deconstructor of a struct? And it is similar to Class?
Thanks so much for your supports,
Upvotes: 1
Views: 615
Reputation: 5409
Quoting from Stroustrup's FAQ
A well-designed class presents a clean and simple interface to its users, hiding its representation and saving its users from having to know about that representation. If the representation shouldn't be hidden - say, because users should be able to change any data member any way they like - you can think of that class as "just a plain old data structure"; for example:
struct Pair {
string name, value; };
A structure is a collection of similar or dissimilar data types. Classes extends the reach of structures by allowing the inclusion of functions within structures. Now, if a structure is just a collection of data-types, you can surely initialize them to some default value just like constructors or else the compiler will do it for you implicitly as mentioned in Vlad's answer but you would not be needing any destructors and there are no destructors by default.
Upvotes: 1
Reputation: 311088
In C++ the notion of the class is defined the following way
class-specifier:
class-head { member-specificationopt}
where class-head in turn is defined like
class-head:
class-key attribute-specifier-seqopt class-head-name class-virt-specifieropt base-clauseopt
class-key attribute-specifier-seqopt base-clauseopt
where
class-key:
class
struct
union
Thus a structure is class with class-key struct
.
And (C++Standard 12.1 Constructors)
4 A default constructor for a class X is a constructor of class X that can be called without an argument. If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted (8.4). An implicitly-declared default constructor is an inline public member of its class...
As a structure is class and does not have a user-declared constructor then such a constructor is declared implicitly by the compiler.
Upvotes: 1
Reputation: 10343
It could simply mean that d1reportAllClassLayout reports them both the same
Upvotes: 0
Reputation: 14583
In C++ class and struct are (almost) exactly the same. The only difference between them is that the default of class is private
and the default of struct is public
Upvotes: 4