Re: calling virtual function results in calling function of base class ...

From: Andreas Lagemann (andreas.lagemann_at_freenet.de)
Date: 01/09/05


Date: Sun, 09 Jan 2005 22:31:13 +0100

Mike Wahler schrieb:
> "Andreas Lagemann" <andreas.lagemann@freenet.de> wrote in message
> news:41e1875a$0$1546$9b622d9e@news.freenet.de...
>
>>Mike Wahler schrieb:
>>
>>>"Andreas Lagemann" <andreas.lagemann@freenet.de> wrote in message
>
>
>>>Misunderstanding. 'Base::foo()' is being called because you
>>>supplied a pointer to a 'Base' object. If you want 'Der::foo'
>>>to be called, you need to suppy the address of (or a reference to)
>>>a type 'Der' object.
>>>
>>
>>Sorry but as far as I understand, I supplied a pointer (sorry for the
>>typo) to Der (i.e. &der) when initializing Bogus. Therefore if I call
>>bogus->bar() Der::foo should be called, shouldnīt it ?
>
>
> 'bogus' is not a pointer to your base class. It's a pointer
> to a type 'Bogus' object.
>
Yes, but Bogus has a member of type Base*, which is initialized with a
pointer to Der.

Oh I just realized that I made another error defining Bogus::bar().
It should be

A* Bogus::bar()
^^
{
   ...
   return myBase->foo();
   ^^^^^^
   ...
}

So any instance of Bogus initialized with a pointer to Der should
execute Der::foo when Bogus::bar() is called. Is that right ?

>
>>Perhaps another Question for clarification:
>>I want to be able to call a specialized versions of a certain function
>>without knowing the exact type of specialization at compile time. That
>>is what I thought virtual functions were intended for ...
>>
>>Can please anyone point out, how I could achieve such a behaviour ?
>
>
> Assign the address of a derived object to a pointer to an object
> of its base class.
>
> #include <iostream>
>
> class Base
> {
> public:
> virtual void f() { std::cout << "base\n"; }
> };
>
> class Derived : public Base
> {
> public:
> void f() { std::cout << "derived\n"; }
> };
>
> int main()
> {
> base *b1 = new Base;
> base *b2 = new Derived;
> b1->f(); /* prints "base" */
> b2->f(); /* prints "derived" */
> delete b1;
> delete b2;
> return 0;
> }

Thatīs just how I thought it should work. So my example should work the
same way, doesnīt it ?

Thank you very much for your patience, Iīm kinda confused by now ...

Andreas



Relevant Pages

  • Re: the virtual keyword
    ... > you could not declare a body for it in the class it was declared virtual, ... This class can only be used as a base class, ... would result in a compiler error. ... function pointer that is added to the actual class. ...
    (comp.lang.cpp)
  • Re: Design problem
    ... Inspect the pointer, if it points to a Frog (or something that implements ... Or to stay with the example: Introducing a spline item. ... >> nor to have a fat interface at the base class. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: calling virtual function results in calling function of base class ...
    ... 'bogus' is not a pointer to your base class. ... Assign the address of a derived object to a pointer to an object ...
    (comp.lang.cpp)
  • Re: Virtual inheritance...
    ... >> slower access to base class members and slower casts (including ... > Just as how an object contains a hidden pointer to a virtual function... ... an object with virtual functions will store ...
    (comp.lang.cpp)
  • Re: Polymorphism
    ... assignment operator was called for aBaseObject and that operator took ... the parts belonging to aDerivedObject's Base class and copied them. ... you only "trick" the system by using a pointer of another type. ... dealing with memory addresses. ...
    (comp.lang.cpp)