By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

about inheritance

P: n/a
Hello!

If you have the following inheritance A is the base class for the derived
class B and B is the base class for the derived class C. In this case will
the constructor of class C be executed first then the constructor of class B
and finally the constructor of class A.

Now to my question. When you have inheritance do you use the assignemt
operator anything?

This is just an example
A::A (const A& a ) : B(a) // here you use the copy constructor
A::A (const A& a ) : B() // here you use use the constructor

Many thanks!

//Tony
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
* Tony Johansson:
Hello!

If you have the following inheritance A is the base class for the derived
class B and B is the base class for the derived class C. In this case will
the constructor of class C be executed first then the constructor of class B
and finally the constructor of class A.
Yes and no: the question is so vague that both answers are (in)valid.

The order of execution is

1. In C's constructor initializer list (if any), the call of the
B constructor (if any such call). This includes evaluation of
arguments for that call. Which might do anything.

2. In B's constructor initializer list (if any), the call of the
A constructor (if any such call). This includes evaluation of
arguments for that call. Which might do anything.

3. A's constructor initializer list, if any.

4. A's constructor body.

5. The rest of B's constructor initializer list, if any. Arguments
specified there for each constructor call are evaluated when that
constructor is called. This rule was the cause of (2).

6. B's constructor body.

7. The rest of C's constructor initializer list, if any. Arguments
specified there for each constructor call are evaluated when that
constructor is called. This rule was the cause of (1).

8. C's constructor body.

Ignoring the constructor initializer lists you can say that the execution
order of constructor _bodies_ is A, then B, then C.

Including the initializer lists you have

BEGIN C constructor (B initializer execution)
BEGIN B constructor (A initializer execution)
BEGIN A constructor (init list execution)
END A constructor (body execution) -- body of A
CONTINUE B constructor (init list execution)
END B constructor (body) -- body of B
CONTINUE C constructor (init list execution)
END C constructor (body) -- body of C

Now to my question.
Wasn't the above your question?

When you have inheritance do you use the assignemt
operator anything?
Use of assignment isn't related to inheritance; they're independent
thing, except when you inherit from a class that's redefined assignment.
This is just an example
A::A (const A& a ) : B(a) // here you use the copy constructor
A::A (const A& a ) : B() // here you use use the constructor


?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

P: n/a
"Alf P. Steinbach" <al***@start.no> wrote in message
news:42*****************@news.individual.net...
* Tony Johansson:


[snip]
When you have inheritance do you use the assignemt
operator anything?


Use of assignment isn't related to inheritance; they're independent
thing, except when you inherit from a class that's redefined assignment.


I'll take a stab that he's asking if the assignment operator is ever used
during construction. Well, I think I have seen assigment-operator calls from
copy constructors, but it's lazy at best and inviting disaster at worst,
especially where inheritance is involved. Even if you get away with it, you
can potentially waste a lot of execution time.
This is just an example
A::A (const A& a ) : B(a) // here you use the copy constructor
I guess you mean: B::B (const B& b ) : A(b)
A::A (const A& a ) : B()
B::B (const B& b ) : A()
// here you use use the constructor


Presumably because you are going to do the copy "construction" with the
assignment operator, since B::operator= and A::operator= happen to do just
what you need. Is that what you mean?

DW

Jul 23 '05 #3

P: n/a
* David White:
"Alf P. Steinbach" <al***@start.no> wrote in message
news:42*****************@news.individual.net...
* Tony Johansson:


[snip]
When you have inheritance do you use the assignemt
operator anything?


Use of assignment isn't related to inheritance; they're independent
thing, except when you inherit from a class that's redefined assignment.


I'll take a stab that he's asking if the assignment operator is ever used
during construction. Well, I think I have seen assigment-operator calls from
copy constructors, but it's lazy at best and inviting disaster at worst,
especially where inheritance is involved. Even if you get away with it, you
can potentially waste a lot of execution time.


Construction isn't assignment.

This is just an example
A::A (const A& a ) : B(a) // here you use the copy constructor
I guess you mean: B::B (const B& b ) : A(b)


I don't know what Tony meant.

A::A (const A& a ) : B()
B::B (const B& b ) : A()
// here you use use the constructor


Presumably because you are going to do the copy "construction" with the
assignment operator, since B::operator= and A::operator= happen to do just
what you need. Is that what you mean?


I don't know what Tony meant.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #4

P: n/a
"Alf P. Steinbach" <al***@start.no> wrote in message
news:42****************@news.individual.net...
* David White:
I'll take a stab that he's asking if the assignment operator is ever used during construction. Well, I think I have seen assigment-operator calls from copy constructors, but it's lazy at best and inviting disaster at worst,
especially where inheritance is involved. Even if you get away with it, you can potentially waste a lot of execution time.
Construction isn't assignment.


I didn't say it was, and this doesn't mean that you can't call the
assignment operator from a copy constructor.
I don't know what Tony meant.
[snip]
I don't know what Tony meant.


I didn't say you did, and I don't know either.

DW
Jul 23 '05 #5

P: n/a
After four days, this reply finally turned up, hence the second attempt.

DW
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.