Thread: base class pointer pointing at derived class

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    base class pointer pointing at derived class

    If i have a base class pointer pointing at a derived class

    Code:
    Base_c * b;
    Derived_c d;
    
    
    b=&d;
    if derived_c redefines a method method()
    if I write b->method() will the derived method be executed?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not unless the base class has the method, as well.
    Preferably it should be virtual as well, so that Derived::method is called instead of Base::method.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    #include <iostream>
    
    class Base_c
    {
        void method(){std::cout << "base" << std::endl;}
        virtual void vMethod(){std:: cout << "base" << std::endl;}
    };
    
    class Derived_c : Base_c
    {
         void method(){std::cout << "derived" << std::endl;}
         void vMethod(){std::cout << "derived" << std::endl;}
    }
    
    int main()
    {
          Base_c *b;
          Derived_c d;
          b = &d;
          b.method();
          b.vMethod();
    }
    try it

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <iostream>
    
    class Base
    {
    public:
        void method(){std::cout << "base" << std::endl;}
        virtual void vMethod(){std:: cout << "base" << std::endl;}
        virtual ~Base() {};
    };
    
    class Derived: public Base
    {
    public:
        void method(){std::cout << "derived" << std::endl;}
        virtual void vMethod(){std::cout << "derived" << std::endl;}
        virtual ~Derived() {};
    }
    
    int main()
    {
          Derived d;
          Base& b = d;
          b.method();
          b.vMethod();
    }
    It also works with references.
    And I would always apply the virtual keyword to a virtual function in a derived class if the base class's function is virtual. The standard may not demand it, but it makes it clearer to the reader.
    C_ntua's example is also wrong. It should be:
    Code:
          b->method();
          b->vMethod();
    And since when did you start using T * instead of T*?
    Last edited by Elysia; 12-01-2008 at 12:26 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    sigh, I always make all kind of little mistakes

    Wasn't aware of the virtual word for derived class. Agreed, much better using it!

  6. #6
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by C_ntua View Post
    sigh, I always make all kind of little mistakes

    Wasn't aware of the virtual word for derived class. Agreed, much better using it!
    thank you
    can you answer also the related theread I posted a few minutes ago?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, you do NOT need to put virtual in the derived class - as long as it's declared virtual in the class that it derives from (infinite number of generations back), it will be virtual. Beware tho' that there will be problems if you change the parameters or return type - as it is no longer the same function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As I said, the standard may not demand it, but I consider it good practice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, more importantly than failing to use the optional virtual keyword for derived class methods, C_ntua forgot to declare the member functions public and use public inheritance.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Horrors o_O
    That's what I get for copying an example.
    At least mine shall be correct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    At least mine shall be correct.
    Where's the virtual base class destructor?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't see a missing virtual destructor, do you?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual base class
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2008, 07:45 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. base class pointer to derived class objects
    By curlious in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2003, 08:39 PM