Click here to Skip to main content
15,901,001 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionhow search engine works Pin
Petra10-Feb-08 6:37
Petra10-Feb-08 6:37 
GeneralRe: how search engine works Pin
Hamid_RT10-Feb-08 21:05
Hamid_RT10-Feb-08 21:05 
Questionhow to make child controls transparent in a non transparent dialog Pin
swarup10-Feb-08 2:35
swarup10-Feb-08 2:35 
AnswerRe: how to make child controls transparent in a non transparent dialog Pin
David Crow11-Feb-08 3:28
David Crow11-Feb-08 3:28 
Generalmake dialog responsive even if it is running a time consuming calculation Pin
swarup10-Feb-08 2:28
swarup10-Feb-08 2:28 
GeneralRe: make dialog responsive even if it is running a time consuming calculation Pin
Mario M10-Feb-08 6:33
Mario M10-Feb-08 6:33 
GeneralRe: make dialog responsive even if it is running a time consuming calculation Pin
Mark Salsbery10-Feb-08 9:48
Mark Salsbery10-Feb-08 9:48 
Generalvirtual function Pin
George_George10-Feb-08 2:00
George_George10-Feb-08 2:00 
Hello everyone,


In the case when function in derived class is virtual, but function in base class is not virtual, when we make base class pointer pointing to derived class' instance, the function in base will be invoked.

I am confused why not the function in Derived class is invoked (this is my question).

When making base pointer pointing to derived class' instance, the layout of derived class should begin with vtable of functions of derived class (virtual function of foo in Derived class in the sample), and the base pointer is pointing to the layout, so when invoking the derived class version function could be found in vtable and it should be invoked. How compiler process this situation internally?

class Base {
public:
	void foo()
	{
		cout << "Base foo " << endl;
	}
};

class Derived: public Base {
public:
	virtual void foo()
	{
		cout << "Derived foo " << endl;
	}
};

int main()
{
	Derived d;
	Base* pb = dynamic_cast<base*>(&d);
	pb->foo(); // output Base foo

	pb = static_cast<base*>(&d);
	pb->foo(); // output Base foo

	pb = &d;
	pb->foo(); // output Base foo
	return 0;
}
</base*></base*>



thanks in advance,
George
GeneralRe: virtual function Pin
BadKarma10-Feb-08 13:03
BadKarma10-Feb-08 13:03 
GeneralRe: virtual functio Pin
Stephen Hewitt10-Feb-08 13:53
Stephen Hewitt10-Feb-08 13:53 
GeneralRe: virtual functio Pin
George_George10-Feb-08 19:19
George_George10-Feb-08 19:19 
GeneralRe: virtual function Pin
George_George10-Feb-08 19:18
George_George10-Feb-08 19:18 
GeneralRe: virtual function Pin
Stephen Hewitt10-Feb-08 13:51
Stephen Hewitt10-Feb-08 13:51 
GeneralRe: virtual function Pin
George_George10-Feb-08 19:20
George_George10-Feb-08 19:20 
Questionhow to assign value of arrary to another one? Pin
gentleguy10-Feb-08 1:38
gentleguy10-Feb-08 1:38 
AnswerRe: how to assign value of arrary to another one? Pin
jeron110-Feb-08 8:21
jeron110-Feb-08 8:21 
GeneralRe: how to assign value of arrary to another one? Pin
gentleguy10-Feb-08 13:34
gentleguy10-Feb-08 13:34 
GeneralRe: how to assign value of arrary to another one? Pin
jeron110-Feb-08 15:30
jeron110-Feb-08 15:30 
GeneralRe: how to assign value of arrary to another one? Pin
gentleguy10-Feb-08 16:12
gentleguy10-Feb-08 16:12 
GeneralRe: how to assign value of arrary to another one? Pin
jeron111-Feb-08 2:25
jeron111-Feb-08 2:25 
QuestionHow to add Button to Child Window ? Pin
tina->newcoder10-Feb-08 1:17
tina->newcoder10-Feb-08 1:17 
GeneralRe: How to add Button to Child Window ? Pin
Hamid_RT10-Feb-08 20:35
Hamid_RT10-Feb-08 20:35 
QuestionRe: How to add Button to Child Window ? Pin
David Crow11-Feb-08 3:31
David Crow11-Feb-08 3:31 
GeneralRe: How to add Button to Child Window ? Pin
tina->newcoder11-Feb-08 5:40
tina->newcoder11-Feb-08 5:40 
QuestionRe: How to add Button to Child Window ? Pin
David Crow11-Feb-08 5:45
David Crow11-Feb-08 5:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.