Solved

Mind Twister......

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Losing latter half of command line in Visual Studio C++ online program 10 89
convert char array to number in c 5 91
Best book to learn C++ 4 87
C++ Code Issue 4 38
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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

730 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