Solved

Mind Twister......

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

943 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