473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

about inheritance

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
5 1667
* 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
"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
* 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
"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
After four days, this reply finally turned up, hence the second attempt.

DW
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephan Diehl | last post by:
I have a question about metaclasses: How would be the best way to "merge" different metaclasses? Or, more precisely, what is the best way to merge metaclass functionality? The idea is basicly...
17
by: Andrew Koenig | last post by:
Suppose I want to define a class hierarchy that represents expressions, for use in a compiler or something similar. We might imagine various kinds of expressions, classified by their top-level...
3
by: arserlom | last post by:
Hello I have a question about inheritance in Python. I'd like to do something like this: class cl1: def __init__(self): self.a = 1 class cl2(cl1): def __init__(self): self.b = 2
2
by: Tony Johansson | last post by:
Hello Experts!! Here we use multiple inheritance from two classes.We have a class named Person at the very top and below this class we have a Student class and an Employee class at the same...
1
by: Tony Johansson | last post by:
Hello Experts! I have some questions about inheritance that I want to have an answer to. It says "Abstract superclasses define a behavioral pattern without specifying the implementation" I...
3
by: Quentin Huo | last post by:
Hi: C# doesn't support multiple inheritance but it supports interface.But I think these ways are different. For example, C++ supports multiple inheritance. I have two classess classA and...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
14
by: JoeC | last post by:
I have been writing games and I also read about good programming techniques. I tend to create large objects that do lots of things. A good example I have is a unit object. The object controls...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
8
by: Tony Johansson | last post by:
Hello! I wonder can somebody explain when is it suitable to use these methods OnKeyUp, OnKeyDown and OnKeyPress because these raise an event. These are located in class UserControl. If these...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.