Derived Class Reference to Base Pointer

V

vwbora

Hi,

Can you please tell me how I can obtain a pointer to a base class from
a variable which is a reference to a derived class.

As an example, I have a variable which is a reference to a derived
class (Manager)and I want obtain a pointer to the base class
(Employee).

Thank you for help.

Pete


class Employee
{
char* name;
int age;
int department;
int salary;

public:
Employee(char* name);
void print();
};


class Manager : public Employee
{
EmployeeList employees;

public:
Manager(char* name, Employee* people);
void print();
};
 
I

Ivan Vecerina

Can you please tell me how I can obtain a pointer to a base class from
a variable which is a reference to a derived class. ....
class Employee
{ ....
};


class Manager : public Employee
{ ....
};


Just use the unary & operator, which returns a pointer to the object
on its right. The conversion from a derived class pointer to a base
class pointer will happen automatically.

void foo( Manager& m )
{
Employee* e = & m;
}



hth-Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top