G
gw7rib
Suppose I have the following code fragment:
class base {
private:
int m;
public:
base(int x) { m = x; }
};
class derived : public base {
private:
otherclass o;
public:
derived(int x) : base(x) { }
};
When I create a derived, is otherclass' constructor run or not? I did
an experiment and it was. However, I'd understood that it wouldn't be.
I thought that *if* I didn't define a default constructor for base
and/or derived, the computer would write one for me which initialised
all the members, but that, in the above case where I have defined a
non-default constructor, it wouldn't start writing constructors for me
(other than a copy constructor). Can you put me straight on this
please? (By the way, the behaviour of otherclass' constructor running
is what I want.)
Thanks in advance.
Paul.
class base {
private:
int m;
public:
base(int x) { m = x; }
};
class derived : public base {
private:
otherclass o;
public:
derived(int x) : base(x) { }
};
When I create a derived, is otherclass' constructor run or not? I did
an experiment and it was. However, I'd understood that it wouldn't be.
I thought that *if* I didn't define a default constructor for base
and/or derived, the computer would write one for me which initialised
all the members, but that, in the above case where I have defined a
non-default constructor, it wouldn't start writing constructors for me
(other than a copy constructor). Can you put me straight on this
please? (By the way, the behaviour of otherclass' constructor running
is what I want.)
Thanks in advance.
Paul.