Derived Class Reference to Base Pointer

Discussion in 'C++' started by vwbora@onetel.com, Apr 17, 2005.

  1. Guest

    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();
    };
     
    , Apr 17, 2005
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > 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
     
    Ivan Vecerina, Apr 17, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tirath
    Replies:
    3
    Views:
    713
    Ivan Vecerina
    Oct 12, 2003
  2. Tim Clacy
    Replies:
    3
    Views:
    593
    Nick Hounsome
    Mar 4, 2004
  3. Replies:
    1
    Views:
    397
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    390
    Victor Bazarov
    May 23, 2007
  5. , India
    Replies:
    8
    Views:
    965
    gwowen
    Aug 18, 2010
Loading...

Share This Page