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? 6 4339
<ma*********@ho tmail.com> wrote in message
news:11******** **************@ i40g2000cwc.goo glegroups.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) {}
}
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?
> 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
> 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?
On Mon, 8 May 2006 05:53:18 -0700, "Jim Langston"
<ta*******@rock etmail.com> wrote in comp.lang.c++: <ma*********@ho tmail.com> wrote in message news:11******** **************@ i40g2000cwc.goo glegroups.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.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
I think you want
Opensource is good to learn sample implementation detail.
Raxit Sheth This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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) .
|
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);
...
}
|
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
class *Shared* methods which make use of the class ID.
So I gave the base class a classID field, and then I gave the
derived classes Shared constructors, which I used to set the classID
field to the appropriate values for each derived class. But...
|
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 parameters, I get CS1501 (no method
with X arguments). Here's a simplified example which mimics the
circumstances:
namespace InheritError {
// Random base class.
public class A {
protected int i;
| |
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 before, been solely 5.0 until now. It seems the 4.4.1 box is
giving me an error on that call.
I've been looking on google for a while for a simple answer, but must
|
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 work an example. Suppose you want your constructor Foo::Foo(char)
to call another constructor of the same class, say Foo::Foo(char,int),
in order that Foo::Foo(char,int) would help initialize the this object.
|
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 temp=0):a(temp){}
A(A& right):a(right.a){}
};
|
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?
class Parent
{
Parent::Parent() { child = new Child( this) };
Child *child;
};
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |