default constructor
Hi,
I couldn't figure out the answer myself about the default constructor --
does C++ create it by default? If yes, under what circumstances?
Actually, I've found out the partial answer myself via trying the
following code:
-----------------------------------------
#include <string>
#include <vector>
class F {
private:
int f;
};
class Fo {
public:
Fo(int i): foo(i) {};
private:
int foo;
};
class Foo {
public:
Foo(int i=0): foo(i) {};
private:
int foo;
};
class Bar: public Foo {
public:
Bar(int i,int j=0): Foo(i), bar(j) {};
private:
int bar;
};
class Bar2: public Foo {
public:
Bar2(int i=0,int j=0): Foo(i), bar(j) {};
private:
int bar;
};
class Monitor {
public:
// interface member functions
std::string::size_type height, width;
private:
std::string vendor;
};
using namespace std;
int main(){
F f;
//Fo f;
Foo foo;
//Bar bar;
Bar2 bar2;
vector<Fo> emptyf; // ok: no need for element default constructor
//vector<Fo> bad(10); // error: no default constructor for Fo
vector<Fo> okf(10, 1); // ok: each element initialized to 1
vector<F> bad(10);
Monitor m;
};
-----------------------------------------
(What's commented out are wrong). But still I don't know exactly under what
circumstances will C++ create default constructor implicitly.
Haven't found the answer from "C++ Primer, 4th Edition" and "The C++
Programming Language, 3rd Edition" yet, and I think it has nothing to do
with the concept of POD (Plain Old Data) object or Trivial Classes defined
in "C++ In A Nutshell".
All in all, the "default constructor" is still a mystery to me, and this
is the only question that I can ask now. Please explain.
Thanks a lot.
tong