Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Derived Class Reference to Base Pointer

Reply
Thread Tools

Derived Class Reference to Base Pointer

 
 
vwbora@onetel.com
Guest
Posts: n/a
 
      04-17-2005
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();
};

 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      04-17-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting derived class pointer to private base class pointer subramanian100in@yahoo.com, India C++ 8 08-18-2010 10:54 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Initialising base class reference members from a derived class Tim Clacy C++ 3 03-04-2004 08:54 PM
[RTTI] cast base class pointer to <templated> derived class pointer tirath C++ 3 10-12-2003 01:44 PM



Advertisments