CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2006
    Location
    Inida
    Posts
    119

    Post why can not we assign base class object reference to derived class pointer?

    class Base
    {
    public:
    Base(int ii = 0 , char cc = 65) : i(ii) , c(cc) {}
    int i;
    char c;
    };

    class Derived
    {
    public:
    Derived(int ii = 0 , char cc = 65 , int id = 10 , char cd = 66) : Base(ii , cc) , i(id) , c(cd) {}
    int i;
    char c;
    };

    void main()
    {
    Base bo;
    Derived *dp = &bo;
    }
    Last edited by adapanaidu; May 10th, 2006 at 05:02 AM. Reason: question is wrong.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: why can not we assign base class object reference to derived class pointer?

    How are Base and Derived related? You might want to compile your sample code to avoid confusion. Oh, and place them between [code] and [/code] forum bbcode tags.

  3. #3
    Join Date
    May 2006
    Posts
    27

    Re: why can not we assign base class object reference to derived class pointer?

    that is because base class does not know about the derived class attributes and methods.

  4. #4
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Post Re: why can not we assign base class object reference to derived class pointer?

    Code:
    class Base
    {
    public:
      Base(int ii = 0 , char cc = 65) : i(ii) , c(cc) {}
    
      int i;
      char c;
        // attributes should preferably be private
    };
    
    class Derived
    {
    public:
      Derived(int ii = 0 , char cc = 65 , int id = 10 , char cd = 66) 
         : Base(ii , cc) , i(id) , c(cd) {}
    
    // but Derived doesn't derive from Base. Did you mean:
    // class Derived : public Base ?
    
    int i;
    char c;
    
    // these "hide" the variables with the same name in the base class, except
    // that Base isn't a base class, and that they shouldn't have been public
    // there in the first place.
    
    };
    
    void main() // oh no, not that again.
    {
      Base bo;
      Derived *dp = &bo; 
     // needs reinterpret_cast. 
    
    }

  5. #5
    Join Date
    Mar 2006
    Location
    Inida
    Posts
    119

    Re: why can we assign base class object reference to derived class pointer?

    Derived is derived from Base;

    class Derived : public Base
    {
    ---------
    ---------
    };

    void main()
    {
    Base bo;
    Derived *pd = &bo; // why is this wrong.
    }

    Please lemme know this

  6. #6
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: why can we assign base class object reference to derived class pointer?

    Quote Originally Posted by adapanaidu
    Code:
    Base bo;
    Derived *pd = &bo; // why is this wrong.
    Because bo is a instance of Base, not an instance of Derived. In other words your're trying to cast it into something it's not.

    - petter

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: why can we assign base class object reference to derived class pointer?

    why is this wrong
    Derived is-a Base, but not vice versa. Though if Base has at least one virtual function, under some circumstances a dynamic_cast might be used, e.g.
    Code:
    Derived *pd = dynamic_cast<Derived*>(&bo);

  8. #8
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: why can we assign base class object reference to derived class pointer?

    dynamic_cast should generally be avoided.

    It is fine in an object-broking system. Otherwise don't use it.

    I use my own object-broking system which works in C++ only. It does not force you to derive your classes from a common base class. It does force you to create a builder for your object and the builder derives from a common base class. There are loads of templates to build most classes you would want. Your class can derive from as many classes as it likes, but the builder can only support one interface, a problem I haven't yet found a solution to
    (that Builder<Derived> and Builder<Base> are unrelated).

    I'm a bit O/T now but basically trying to explain that you should not generally cast down the hierarchy, but there are sometimes when it's necessary. In an object-broking system you load them dynamically at run-time from a string, and you have to be able to check that you were given an object of the type you really want.

  9. #9
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: why can we assign base class object reference to derived class pointer?

    In the example given in the OP, dynamic_cast will not work, no matter how many virtual functions it has. A Base is-not-a derived, in the same way that an Animal is-not-a Dog. (But a Dog is-a Animal.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured