Thread: Nooblet question regarding inheritance

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    41

    Nooblet question regarding inheritance

    Just a quick question regarding class hierarchy movement... I've googled and looked a tutorials, but just thought a human point-of-view would be more reliable as my exact answer I did not find.

    I'm trying to figure out why the following doesn't work.. the dynamic_cast I would think should work, but it complains that the pointer being dynamically cast isn't a polymorphic type. I would think since dynamic_cast is a run-time thing it would recognize that I'm trying to cast from class C up to class B. Anyways my ultimate questions are:

    1 - If the pointer is to a base class can I safely use new to have it create a derived class.
    Code:
    A *test = new C;  // <--- This valid?  I assume it is
    2 - If I were to replace the dynamic_cast in the following code with a non-checked cast is it valid?
    Code:
    A *test = new C;  B *foo = (B*)(test);
    Code:
    class A{
    public:
      void set(int z){a = z;};
      int a;
    };
    
    class B : public A{
    public:
      int c;
      void hello(){};
      void set(int z){b = z;};
      int b;
    };
    
    class C : public B{
    public:
      void set(int z){c = z;};
      int c;
    };
    
    
    int main()
    {
      A *test = new C;
    
      B *foo = dynamic_cast<B*>(test);  // I understand why it fails because the pointer is technically of class A, but is this safe?  
      foo->set(1);
      delete test;
    
      return 0;
    }
    Last edited by HyperShadow; 04-26-2008 at 09:38 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    1 - If the pointer is to a base class can I safely use new to have it create a derived class.
    Yes, but if you use new, you should use delete... and if you delete an object of a derived class via a pointer to its base class, the base class destructor must be virtual or the behaviour is undefined.

    2 - If I were to replace the dynamic_cast in the following code with a non-checked cast is it valid?
    In this case since you know that the underlying object is of a derived class of B, it should work... but why use a C-style cast, and if you do not need it, why use dynamic_cast?
    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

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your classes don't contain virtual functions. In particular, if polymorhism was at work here, all set's should call C::set. Without the virtual keyword they don't.
    Code:
    #include <iostream>
    
    class A{
    public:
      //virtual  //uncomment to see the difference
      void set(int z){std::cout << "A::set\n"; a = z;};
      int a;
    };
    
    class B : public A{
    public:
      int c;
      void hello(){};
      void set(int z){std::cout << "B::set\n"; b = z;};
      int b;
    };
    
    class C : public B{
    public:
      void set(int z){std::cout << "C::set\n"; c = z;};
      int c;
    };
    
    
    int main()
    {
      A *test = new C;
      test->set(1);
      B *foo = static_cast<B*>(test);  // I understand why it fails because the pointer is technically of class A, but is this safe?
      foo->set(1);
      C *bar = static_cast<C*>(test);
      bar->set(1);
      delete test;
    
      return 0;
    }
    However, if that is what you want, it seems that you can create a simple instance and call the desired version of set manually (not that it seems like a really great way to use classes and inheritance):
    Code:
    #include <iostream>
    
    class A{
    public:
      void set(int z){std::cout << "A::set\n"; a = z;};
      int a;
    };
    
    class B : public A{
    public:
      int c;
      void hello(){};
      void set(int z){std::cout << "B::set\n"; b = z;};
      int b;
    };
    
    class C : public B{
    public:
      void set(int z){std::cout << "C::set\n"; c = z;};
      int c;
    };
    
    
    int main()
    {
      C test;
      test.set(1);
      test.B::set(1);
      test.A::set(1);
    
      return 0;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Unless you are simply experimenting to see how dynamic_cast works, forget about it until you really think you need it. Read about polymorphism and the Liskov Substitution Principle. That is how you should be using public inheritance.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I understand why it fails because the pointer is technically of class A, but is this safe?
    If it fails, how can it be safe? But you apparently don't understand why it fails. The reason it fails is that A has no virtual functions and as such is not polymorphic - dynamic_cast simply won't work with it. The compiler should fail to compile this:
    Code:
    dyn.cpp:14: error: cannot dynamic_cast ‘a’ (of type ‘struct A*’) to type ‘struct B*’ (source type is not polymorphic)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob question about templates & inheritance
    By blacknail in forum C++ Programming
    Replies: 9
    Last Post: 10-25-2008, 01:51 PM
  2. Virtual inheritance
    By 6tr6tr in forum C++ Programming
    Replies: 13
    Last Post: 05-07-2008, 11:20 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM