Solved

Mind Twister......

Posted on 2003-11-04
4
304 Views
Last Modified: 2010-04-02
Hi,
  I got this question from one of the user group.. I found it quite interesting.... So I thought you people must give it a try.... I guess i know the answer but i am not that confident about it...... Its just a puzzle to puzzle you.....

class base
{
public:

      virtual void func1 ()
      {
            cout << "Base: func1" << endl;
      }
      
      void func2 ()
      {
            cout << "Base: func2" << endl;
      }
};

class derived
{
public:
      void func1 ()
      {
            cout << "Derived: func1" << endl;
      }
      
      virtual void func2 ()
      {
            cout << "Derived: func2" << endl;
      }
};


int main(void)
{
      base* pb1;
      derived d1;

      pb1=(base*)&d1;

      pb1->func1 ();
      pb1->func2 ();

 return 0 ;
}

Guess the output first..... And then try the explaination part......

Don't think the answer but know the answer ... ;-)

Dennis
0
Comment
Question by:dennis_george
4 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 25 total points
ID: 9684344
The result of this code is undefined according to the C++ standard.

The base class is not a base class of the derived class.
So forcing the compiler to use the wrong class type pointer for your object leads to undefined behavior.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9684872
I agree with Axter.  As shown, you might as well call strcpy(), with the two parameters being a pointer to a function and a pointer to a void pointer... something might happen, but whatever it is, would be meaningless.

Perhaps the question is mis-stated.  If the object named 'derived'  is actually derived from the object named 'base' then the question might have some meaning.

-- Dan
0
 

Expert Comment

by:gwap
ID: 9685533
Like axter said, the result is undefined according to the standard, however, due to the most common implementation of the C++ standard, your output should be :
Derived:func2
Base:func2

I don't need to explain the Base:func2 outcome as it is merely a static link.
The Derived:func2 outcome is because, for class base, the pb1->func2 call is translated to "the first function in the virtual function pointer table."

This virtual function pointer table is the table of Derived; furthermore, the first (and only) entry in the table of Derived is that of virtual function Derived::func2.

But like Axter said, this is implementation defined.
0
 
LVL 4

Expert Comment

by:havman56
ID: 9687095
dennis,

Axter said is absoluetly correct behaviour undefined.

kindly dont try to test experts  knowledge in EE.
see section 10.3 of C++ standard

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now