Thread: Inheritance: assign base class to derived class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) A derived type can be assigned to a variable of type base. That's because a base variable can be used to call all public functions and member variables in the base class. Since a derived object has all those same functions and member variables as the base class, calling those functions and member variables for a derived object that is masquerading as a base object will succeed.

    2) However, you cannot assign a base type to a derived class type. That's because a variable of type derived can be used to call any public function or member variable in the derived class. But if a base object is hiding in a variable of type derived, calling some functions and member variables in the derived class will not succeed because none of the functionality added by the derived class will be present in a base object.

    The bottom line is: transpose() returns a Matrix object, so your variable must be of type Matrix. Why would you expect that a Matrix object returned by transpose() to have the functionality of a Matrix3D object? You can't squeeze lemons and get orange juice.
    Last edited by 7stud; 01-22-2007 at 12:25 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Since all you want to do is call a function in the base class, a solution is to assign your derived object to a base type variable, and then use the base variable to call transpose().
    Let's say I have the following: Matrix3D A = B * (C * B.transpose()); B and C are both Matrix3D. If I were to assign the B.transpose() to a base type, A, B, and C would be base types and my problem would not exist. However, I find the use of a Matrix class undesirable for reasons previously stated. Is there any possibility to go about this another way, except for reimplementing transpose in the Matrix3D class?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  2. Inheritance - Problem calling methods
    By rusty_turkey in forum C++ Programming
    Replies: 5
    Last Post: 05-31-2007, 04:36 AM
  3. simple question
    By SuperNewbie in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2005, 03:03 PM
  4. Replies: 1
    Last Post: 11-27-2001, 01:07 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM