[comp.lang.c++] Question regarding multiple inheritance and virtual functions..

mohans@caterina.Eng.Sun.COM (Ram Mohan Srinivasan) (03/28/91)

I took a class in c++ and in doing one of the assignments, I came across
the following problem.. I don't know what I'm doing wrong or whether
this is the way things are supposed to work.. Can someone explain this 
to me ?

Before U flame me for asking a dumb question - I am a novice c++ pgrmr -
And that's my excuse :-)

I have the following class declarations :

class employee {
	int emp_no;
	char *emp_name;
public:
	employee();
	~employee();
	virtual void print();
};

class employee_pl_addr : public employee {
	char *address;
public :
	employee_pl_addr();
	~employee_pl_addr(); 
	void print(); 
};

class home_addr {
	char *address; 
	char *tel_no;
public :
	home_addr();
	~home_addr();
	virtual void print();
};

class home_and_work : public home_addr, public employee_pl_addr {
public :
	home_and_work() {}
	~home_and_work() {} 
};

#Irrelevent stuff deleted..
#
#The print() function in each class, just prints the members..

In my main program, I am trying to get at the virtual function print thus :

	home_and_work emp;

	((employee *) &emp)->print();
	((employee_pl_addr *) &emp)->print();
	((home_addr *) &emp)->print();

The problem that I have is - 

I do not seem to be able to get at the class employee's print virtual function 
by 
	((employee *) &emp)->print();
instead, this calls the print() function of the class employee_pl_addr !

Has this something to do with class scopes ? I was under the impression
that we could statically determine which virtual function is to be
called in all the 3 above cases.. The examples given in my text call 
virtual functions this way.

Or is it that we can get at virtual functions just one level up ? 

Oh, and I am using the C++ translator on SunOS 4.1..



thanks,

Mohan Srinivasan.
mohans@sun.com
--

===============================================================================

Ram Mohan Srinivasan			(415)-336-1272 (W)

mvm@jedi.harris-atd.com (Matt Mahoney) (03/29/91)

In article <10614@exodus.Eng.Sun.COM> mohans@caterina.Eng.Sun.COM
 (Ram Mohan Srinivasan) writes:

>I do not seem to be able to get at the class employee's print virtual function 
>by 
>	((employee *) &emp)->print();
>instead, this calls the print() function of the class employee_pl_addr !

Try:	 emp.employee::print();

-------------------------------
Matt Mahoney, 407-727-5431, mvm@epg.harris.com
#include <disclaimer.h>  // disclaimer.hpp in Zortech