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

base class's para. const. not called from the virtual derived

Please find the below program executed in SUNOS


Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2.  
  3. class A
  4. {
  5. public:
  6. int x,y;
  7. A(){cout<<"\n DefaultConst A";}
  8. A(int i, int j){cout<<"\n Parm const A"; x=i; y=j;}
  9. };
  10.  
  11. class B : virtual public A
  12. {
  13. public:
  14. int a, b;
  15. B():A(150,170){cout<<"\n DefaultConst B";}
  16. };
  17.  
  18. class C: public B
  19. {};
  20. int main()
  21. {
  22. A *ptr;
  23. C *pobc=new C();
  24. pobc->a=5; pobc->b=2;
  25. printf("\n %d %d %d %d", pobc->x, pobc->y,pobc->a, pobc->b);
  26. }

Output
------
DefaultConst A
DefaultConst B
0 0 5 2


Question
---------
1) Why doesn't the parametrized constructor of A is being called and why x and y are being initialized with 150 and 170
Jan 6 '10 #1
3 1448
newb16
687 512MB
Because for virtual base class default constructor is called because in "diamond pattern" there is only one instance of such base class.
Jan 6 '10 #2
Could you make me understand "diamond pattern". Atlest some hints? Thank in Advance
Jan 6 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
This example does not need virtual base classes.

Virtual base classes are used only if there is a chance that a base class constructor will be called more than once during the creation of a derived object.

For example:

A derives from X
B derives from X
C derives from A and B.

In this case, creating an object of C requires creating an object of B which requires creating an object of X. It also requires creating an object of A which requires creating an object of X. There are now two X objects inside the C object. This is the "diamond" hierarchy.

This may be good or it may be bad depending upon your design.

If you require a single X object you have you use virtual base classes. And when you do that, the compiler will not call any constructors. Instead, you have to call them yourself.

So, in your example, because of that virtual base class, you need a C constructor that calls the exact constructors you need to build a C object:

Expand|Select|Wrap|Line Numbers
  1. C() : A(150, 170), B() {}
You have to remember that virtual base classes disable the constructor calls to ancestor (indirect) base classes.
Jan 6 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
20
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
4
by: Xavier | last post by:
Hi, I have a question, in a "dreaded diamond" situation, regarding the following code: ---- Begin code #include <iostream> using namespace std;
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
8
by: Mike - EMAIL IGNORED | last post by:
I have a class that may or may not be inherited. Its constructor calls a function that should be called only if the class is not inherited. Is there a way to tell, other than simply passing a...
5
by: Dimitry | last post by:
I am trying to make the following: struct Base { char param; }; class Derived1 : public Base { public:
6
by: MattWilson.6185 | last post by:
Hi, I'm trying to find out if something is possible, I have a few diffrent lists that I add objects to and I would like to be able to have a wrapper class that won't affect the internal object, for...
10
by: Dennis Jones | last post by:
Hello, I have a hierarchy of classes in which there will be a data element that is common to all descendant classes. This element (a string in this case) is constant, but specific to each...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
1
by: Rune Allnor | last post by:
Hi all. I am sure this is an oldie, but I can't find a useful suggestion on how to solve it. I have a class hierarchy of classes derived from a base class. I would like to set up a vector of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.