473,382 Members | 1,107 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,382 software developers and data experts.

Constructor question, how does the call to the parent constructor work?

Say I have class A that inherits from class B, and I call class A his
constructor. Then somehow class B his constructor is called also. How
does this work? Is a constructor under the hood a sort of virtual
method by default, and are the ancestors constructors called in a sort
of VT table?

May 8 '06 #1
6 4322
<ma*********@hotmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Say I have class A that inherits from class B, and I call class A his
constructor. Then somehow class B his constructor is called also. How
does this work? Is a constructor under the hood a sort of virtual
method by default, and are the ancestors constructors called in a sort
of VT table?


A base's constructor will be called whenever a derived class's constructor
is called.

That is:

class Base
{
public:
Base() { std::cout << "Base Constructor" << std::endl; }
}

class Derived: Base
{
public:
Derived() { std::cout << "Derived Constructor" << std::endl; }
}

void main()
{
Derived MyDerived; // At this point Base Constructor is called and
Derived Constructor
}

If a Base does not have a default constructor, you'll need to supply it's
paramters in the derived initialization list.

class Base
{
public:
Base( int i ): i_(i) {}
}

class Derived: Base
{
public:
Derived( int i ): Base(i) {}
}

May 8 '06 #2
Jim

[some things on the result of constructor calling]

Thanks I understand that part now. But my remaining question is: does
this mean that the constructor actually is a virtual function, and that
the parent constructor is found by the VT-table? And if this is true,
then we never actually use the word virtual in declaring a constructor,
but it in fact is a virtual method. You would just not use the virtual
keyword, since all constructors have to be virtual anyway.

Or am I way off now?

May 8 '06 #3
> Thanks I understand that part now. But my remaining question is: does
this mean that the constructor actually is a virtual function, and that
the parent constructor is found by the VT-table?


Why do you think it needs a virtual table? When calling constructor of base
class, compiler has all the information needed, and makes the decision what
to call in compile time. There's no need for any runtime mechanisms like
virtual functions.

Marcin


May 8 '06 #4
> Why do you think it needs a virtual table?
When calling constructor of base class, compiler has all the information needed


You're very right! And it would take up space even, and be inefficient.

Actually, and that is entirely off topic...., I am confused by the
concept of a virtual constructor in a language like Delphi/ObjectPascal
were they do have a virtual constructor. Now I wonder why Delphi does
have a virtual constructor. What the use, since you would know
information compile time on creation of an object of a certain type?

May 8 '06 #5
On Mon, 8 May 2006 05:53:18 -0700, "Jim Langston"
<ta*******@rocketmail.com> wrote in comp.lang.c++:
<ma*********@hotmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Say I have class A that inherits from class B, and I call class A his
constructor. Then somehow class B his constructor is called also. How
does this work? Is a constructor under the hood a sort of virtual
method by default, and are the ancestors constructors called in a sort
of VT table?


A base's constructor will be called whenever a derived class's constructor
is called.

That is:

class Base
{
public:
Base() { std::cout << "Base Constructor" << std::endl; }
}

class Derived: Base
{
public:
Derived() { std::cout << "Derived Constructor" << std::endl; }
}

void main()


[snip]

Of course the line above makes the program ill-formed and no longer
C++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
May 10 '06 #6

I think you want
Opensource is good to learn sample implementation detail.

Raxit Sheth

May 10 '06 #7

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

Similar topics

7
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
24
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
10
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
7
by: Greg Scharlemann | last post by:
I have a class with a constructor function that looks like so on my php 5.0 box: public function __construct(....) does this syntax change when I go back to php 4.4.1? I've never used 4.4.1...
7
by: gopal | last post by:
Can one constructor of a class call another constructor of the same class to initialize the this object? I read in the FAQ , to the above question the following ans was given Nope. Let's...
5
by: flamexx7 | last post by:
In the programme below is it possible to call copy constructor of class A, inside copy constructor of class B. #include <iostream> using namespace std; class A{ int a; public: A(int...
4
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called?...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.