Thread: A simple C++ inherience code sinpet: how could this happen?!

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    A simple C++ inherience code sinpet: how could this happen?!

    Code:
    class Base{
    public: 
    	void foo(){cout<<"base foo"<<endl;}
    };
    
    class Derived: public Base{
    public:
    	void foo(){cout<<"derivde foo"<<endl;}
    };
    
    void test(Derived*  obj){
    	obj->foo();
    }
    int main(){	
    	Base obj;
    	test((Derived*)&obj);	
    }
    I expect output to be "base foo", but it's "derivde foo".
    My understanding is that obj is a Base type, no matter what type you cast it to. How could a base type refer to a derived type function?
    Last edited by meili100; 10-11-2009 at 02:44 PM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You need the base function to be virtual.

    Code:
    class Base{
    public: 
    	virtual void foo(){cout<<"base foo"<<endl;}
    };
    But still, this shouldn't work the way you use it. You have a base class obj. If you cast it it magically turns to a derived class? Think this:

    Code:
    class Base{
    public: 
    	void foo(){cout<<"base foo"<<endl;}
    };
    
    class Derived: public Base{
    public:
            int a;
    	void foo(){cout<< a <<endl;}
            Derived() {a = 1;}
    };
    
    void test(Derived*  obj){
    	obj->foo();
    }
    int main(){	
    	Base obj;
    	test((Derived*)&obj);	
    }
    Now you understand that if you allocate a Base object it wont have the variable a. So how would it be able to call foo from the derived class?

    You can go from Devired to Base, not the other way around!

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    1
    Quote Originally Posted by meili100 View Post
    I expect output to be "base foo", but it's "derivde foo".
    My understanding is that obj is a Base type, no matter what type you cast it to. How could a base type refer to a derived type function?
    In memory, those bytes can only be the size and shape of a Base type.
    However, in casting the pointer, you've insisted that, never mind what the
    compiler thinks is at that address, you know better and the bytes at that
    address are to be interpreted as a Derived. This is the kind of weirdness you get
    when you use casts, and why casts are considered an unsavory programming
    technique.

    In this snippet, you've used the old C-style cast. If you had tried to use on of the
    C++ cast types (static_cast, dynamic_cast), you would have gotten a warning that
    this cast doesn't make sense. If you used reinterpret_cast, the compiler still would
    have shrugged and said, "Okay, if you think you know what you're doing, go ahead
    and shoot yourself in the foot."

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You probably get away with this only because this method doesn't touch *this - e.g access any members.

    You will probably get the same result when you replace main with

    Code:
    int main(){
    	int obj;
    	test((Derived*)&obj);
    }
    It just so happens that test knows it has to call Derived::foo, and the call just happens to succeed, because it doesn't need anything from the object itself.

    Just to give you some idea how wrong this is.
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple C code problem
    By neo28 in forum C Programming
    Replies: 24
    Last Post: 05-16-2009, 10:48 AM
  2. Simple C++ question. Error in code somewhere.
    By Paracropolis in forum C++ Programming
    Replies: 10
    Last Post: 02-06-2006, 08:59 AM
  3. Replies: 14
    Last Post: 11-23-2005, 08:53 AM
  4. problem with some simple code =/
    By Josh Norton in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 06:27 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM