473,383 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Derived Class Reference to Base Pointer

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
1 1531
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Victor Hannak | last post by:
I have two classes derived from a base class. The two derived classes each utilize a structure that is slightly different from one another. i.e. DerivedClass1: struct NodeStruct { float...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
13
by: Moes | last post by:
struct Base{ }; struct Derived: public Base{ }; void f( Base *pBase ) { Derived *pDerived = (Derived*)pBase; }
10
by: Julia | last post by:
Hi Please can someone explain this behaviour: I have a MustInherit Base class and a Derived class that Inherits Base and Shadows a method in the base class. If I Dim a variable of type...
3
by: Bangalore | last post by:
Hi, In the following program, eventhogh two member function declared under private section of the derived class are accessable by derived class pointer. Please clarify me how can derived class...
9
by: Mirko Puhic | last post by:
Is there a way to properly do this? struct Derived; struct Base{ Derived der; };
2
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
10
by: Dom Jackson | last post by:
I have a program which crashes when: 1 - I use static_cast to turn a base type pointer into a pointer to a derived type 2 - I use this new pointer to call a function in an object of the...
3
by: Immortal Nephi | last post by:
The rule of inheritance states that you define from top to bottom. Sometimes, you want to define base class and set reference from dervied class to base class, but you violate the rule. Here is an...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.