about inheritance

T

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.

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
 
A

Alf P. Steinbach

* 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

?
 
D

David White

Alf P. Steinbach said:
* 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.

I guess you mean: B::B (const B& b ) : A(b)

B::B (const B& b ) : A()

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

DW
 
A

Alf P. Steinbach

* David White:
Alf P. Steinbach said:
* 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.

I guess you mean: B::B (const B& b ) : A(b)

I don't know what Tony meant.

B::B (const B& b ) : A()


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

I don't know what Tony meant.
 
D

David White

Alf P. Steinbach said:
* David White:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top