structs in C vs C++
If you give no constructor or destructor a default one will be provided. All this does is call the default constructor for all member variables (i.e. it does nothing if there are only primitive member variables).
quote:Original post by vicviper
I''ve been using c++ structures with member functions for some time... one thing it puzzles me is:
although you can define constructors and destructors for a structure... what if you don''t define any?
if you do this:
class xxx {};
xxx arrayx[100]; // here, a constructor is called 100 times
struct yyy {};
yyy arrayy[100]; // just a plain sizeof(yyy)*100 memory allocation, or 100 constructor calls also?
If you don''t provide constructor/destructor for your classes or structures, compiler generates them. In that case those methods will do absolute nothing and they are inlined.
so:
struct myStructure
{
int x;
};
equals to:
struct myStructure2
{
myStructure2() {}
~myStructure2() {}
int x;
};
PS. There are also couple of other methods/operators that are automaticly generated by the compiler if you don''t provide them.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement