Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: calling virtual function from base class

Reply
Thread Tools

Re: calling virtual function from base class

 
 
Phui Hock
Guest
Posts: n/a
 
      09-08-2004
I've found something funny

#include <iostream>
using namespace std;

class Base
{
public:

void EatPoo()
{
cout << "Base::EatPoo" << endl;
}
};

class NotBaseDerived
{
public:
void EatPoo()
{
cout << "NotBaseDerived::EatPoo" << endl;
}
};

int main()
{
NotBaseDerived nbd;

Base* r = reinterpret_cast<Base*>(&nbd);

r->EatPoo();
return 0;
}

The output is Base::EatPoo(). I know this is gonna break the program if
Im builing one like this but the idea is compiler already did early
binding for r before the program is actually run

 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      09-08-2004
Phui Hock posted:

> I've found something funny
>
> #include <iostream>
> using namespace std;
>
> class Base
> {
> public:
>
> void EatPoo()
> {
> cout << "Base::EatPoo" << endl;
> }
> };
>
> class NotBaseDerived
> {
> public:
> void EatPoo()
> {
> cout << "NotBaseDerived::EatPoo" << endl;
> }
> };
>
> int main()
> {
> NotBaseDerived nbd;
>
> Base* r = reinterpret_cast<Base*>(&nbd);
>
> r->EatPoo();
> return 0;
> }
>
> The output is Base::EatPoo(). I know this is gonna break

the program if
> Im builing one like this but the idea is compiler already

did early
> binding for r before the program is actually run
>
>


Exactly, and why? Because you working with it in the
context of a "Base".

One thing though, even though neither of your classes are
polymorphic (by which I mean that they have no virtual
functions), and even though you have no member variables,
still I don't think you can be certain that your code is
"defined behaviour". While it may sound absurd, according
to the Standard, your compiler MAY store objects in memory
in whatever fashion tickles its fancy. As such, even though
there's no member variables, the compiler has the choice of
storing some data wherever it wants. One of these weird
methods chosen by the compiler may result in a scenario in
which your code may not work as expected.

Anyway, long story short, your code exhibits undefined
behaviour (although I leave myself to be corrected if by
any chance there's some subtlety I've missed)


-JKop
 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      09-08-2004
"Phui Hock" <(E-Mail Removed)> wrote in message news:<chmb0n$(E-Mail Removed)>...

[ ... ]

> NotBaseDerived nbd;
>
> Base* r = reinterpret_cast<Base*>(&nbd);
>
> r->EatPoo();


What you have here is simple undefined behavor, so the compiler is
free to do anything it feels like. Consider it pure (bad) luck that
you got any output at all instead of the program simply crashing.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling virtual function results in calling function of base class... Andreas Lagemann C++ 8 01-10-2005 11:03 PM
calling virtual function results in calling function of base class ... tiwy C++ 0 01-09-2005 11:17 PM
calling virtual function from base class Phui Hock C++ 4 09-07-2004 07:21 PM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 2 12-01-2003 06:31 AM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 3 11-30-2003 12:18 AM



Advertisments