Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Mind Twister......

Posted on 2003-11-04
4
Medium Priority
?
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 75 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

636 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