Re: calling virtual function results in calling function of base class ...
From: Andreas Lagemann (andreas.lagemann_at_freenet.de)
Date: 01/09/05
- Next message: R. Stormo: "Re: Q:show output from script"
- Previous message: Jerry Coffin: "Re: size_t and int comparison"
- In reply to: Mike Wahler: "Re: calling virtual function results in calling function of base class ..."
- Next in thread: Mike Wahler: "Re: calling virtual function results in calling function of base class ..."
- Reply: Mike Wahler: "Re: calling virtual function results in calling function of base class ..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: R. Stormo: "Re: Q:show output from script"
- Previous message: Jerry Coffin: "Re: size_t and int comparison"
- In reply to: Mike Wahler: "Re: calling virtual function results in calling function of base class ..."
- Next in thread: Mike Wahler: "Re: calling virtual function results in calling function of base class ..."
- Reply: Mike Wahler: "Re: calling virtual function results in calling function of base class ..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|