By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Derived Class Reference to Base Pointer

P: n/a
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();
};

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
<vw****@onetel.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.