base class parm. const not called from virtual derived class

Post questions regarding programming in C/C++ in Linux/Unix.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

base class parm. const not called from virtual derived class

Postby mathew1 » Wed Jan 06, 2010 4:39 am

Please find the below program executed in SUNOS


#include<iostream.h>

class A
{
public:
int x,y;
A(){cout<<"\n DefaultConst A";}
A(int i, int j){cout<<"\n Parm const A"; x=i; y=j;}
};

class B : virtual public A
{
public:
int a, b;
B():A(150,170){cout<<"\n DefaultConst B";}
};

class C: public B
{};
int main()
{
A *ptr;
C *pobc=new C();
pobc->a=5; pobc->b=2;
printf("\n %d %d %d %d", pobc->x, pobc->y,pobc->a, pobc->b);
}


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


Question
---------
1) Why doesn't the parametrized constructor of A called and why x and y are not initialized with 150 and 170
mathew1
 
Posts: 2
Joined: Wed Jan 06, 2010 4:25 am

Re: base class parm. const not called from virtual derived class

Postby Alvaro » Wed Jan 06, 2010 7:50 am

User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA


Return to Unix/Linux

Who is online

Users browsing this forum: No registered users and 0 guests