Solved

Mind Twister......

Posted on 2003-11-04
4
327 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java inner class, for the sole use of parameter passing 21 77
C Language combined operators 28 114
thread-safe code in c++ 2 115
C++ help/ Toy problem 19 28
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

808 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