Rolf Magnus wrote:
Tom wrote:
I was reading Bjourne's book and wonder about constructors?
If I have a class
template<class T> class Vector {
public:
explicit Vector(size_t n);
}
Does a default constructor get generated by the compiler?
No. Whenever you define any constructor for a class (or class template), the
compiler will not generate a default constructor for it.
Whenever you declare, rather than define, any constructor ...
The most obvious example of declaring but not defining a constructor
that springs to mind is disabling copying by declaring the copy
constructor (and assignment operator, but that has no effect either way
on the compiler generated default constructor) private.
// Don't need any special initialisation so I'll
// rely on the compiler generated default constructor
class foo
{
public:
// stuff (but no constructors) ...
private:
// Disable copying
// No definition is provided anywhere for these two
foo(const foo&);
foo& operator=(const foo&);
// other stuff (but no constructors) ...
};
int main()
{
foo f; // Error: no default constructor available
}
Gavin Deane