Satish Sangapu wrote:
I am reading "Inside the C++ Object Model" by Stanley Lippman.
It is a very nice book about the internals of C++.
Anyway, I am confused about one thing. He states that
a default constructor is not ALWAYS generated by the compiler -
ONLY when it's NEEDED and then he goes on to define NEEDED.
class one {
private:
int a;
bool b;
};
Apparently, the above class won't have
a default constructor generated by the compiler.
Wrong.
But,
class data {
public:
data(void);
};
class two {
private:
data d;
};
The above two class will have
a default constructor generated by the compiler.
Wrong.
The default constructor for class data
*must* be provided by you -- the programmer.
The book was written in 1996.
I wonder if stuff like this has changed by now?
Nothing has changed in this regard.
C++ compilers will "provide" a default constructor,
a copy constructor and an assignment operator
if you don't declare them.
You need to be careful about what you mean by
"generated by the compiler".
C++ compilers don't need to write a default constructor.
All they really need to do is emit code to construct the object.
There may be no code that you can identify as
"a call to the default constructor". It might be "as if"
you defined all of your constructors inline
and all of the code was "optimized away".
The notion that the compiler "provided" or "generated"
a default constructor may be purely conceptual.