Click here to Skip to main content
15,911,142 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: DT_CALCRECT with DT_WORDBREAK Pin
Code-o-mat3-Feb-09 0:42
Code-o-mat3-Feb-09 0:42 
GeneralRe: DT_CALCRECT with DT_WORDBREAK Pin
_T("No name")3-Feb-09 1:02
_T("No name")3-Feb-09 1:02 
QuestionWhy does my dialog controls disappear ? Pin
kapardhi3-Feb-09 0:08
kapardhi3-Feb-09 0:08 
AnswerRe: Why does my dialog controls disappear ? Pin
Cedric Moonen3-Feb-09 0:12
Cedric Moonen3-Feb-09 0:12 
GeneralRe: Why does my dialog controls disappear ? Pin
kapardhi3-Feb-09 0:17
kapardhi3-Feb-09 0:17 
GeneralRe: Why does my dialog controls disappear ? Pin
CPallini3-Feb-09 0:28
mveCPallini3-Feb-09 0:28 
GeneralRe: Why does my dialog controls disappear ? Pin
Cedric Moonen3-Feb-09 0:56
Cedric Moonen3-Feb-09 0:56 
QuestionWhat is complier actually doing here ? Pin
ComplexLifeForm2-Feb-09 23:56
ComplexLifeForm2-Feb-09 23:56 
Hi All,

I have a query related to virtual methods and access specifiers in C++ class. I have a two classes and their definition is as shown below

class Base
{
public:
virtual void PrintMethod()
{
cout << "Base class method called";
}
};

class Derived : public Base
{
private:
virtual void PrintMethod()
{
cout << "Derived class method called";
}
};

in main method

int main()
{
Derived der;
Base *ptr = &der;

ptr->PrintMethod(); ------------ (1)

// der.PrintMethod(); ------------ (2)

return 0;
}

When I execute the program the output I get is "Derived class method called" and this confused me a bit. I could not figure out properly why the derived class method was called when it is private in class Derived?

My guess is that the public function in class Base is inherited in Derived class and when the call was made the through the pointer the linker could only find the private definition of the function PrintMethod and hence it called it through the pointer which is actually pointing to the Derived class object.

If I try to call the function through the derived class object (as shown in 2), I get compilation error saying the private method is not accessible which is true.

So overall I was not satisfied with the reasoning I arrived at for the derived class method being called.
Can someone give me more insight into what actually is happening here?

Thanks and Regards
Smile | :) Smile | :)
AnswerRe: What is complier actually doing here ? Pin
Cedric Moonen3-Feb-09 0:04
Cedric Moonen3-Feb-09 0:04 
AnswerRe: What is complier actually doing here ? Pin
CPallini3-Feb-09 0:05
mveCPallini3-Feb-09 0:05 
AnswerRe: What is complier actually doing here ? Pin
«_Superman_»3-Feb-09 1:10
professional«_Superman_»3-Feb-09 1:10 
Questionfloating point precision issue Pin
Sarath C2-Feb-09 23:42
Sarath C2-Feb-09 23:42 
AnswerRe: floating point precision issue Pin
Cedric Moonen2-Feb-09 23:53
Cedric Moonen2-Feb-09 23:53 
GeneralRe: floating point precision issue [modified] Pin
Sarath C3-Feb-09 0:38
Sarath C3-Feb-09 0:38 
GeneralRe: floating point precision issue Pin
Cedric Moonen3-Feb-09 0:48
Cedric Moonen3-Feb-09 0:48 
GeneralRe: floating point precision issue Pin
Sarath C3-Feb-09 0:55
Sarath C3-Feb-09 0:55 
GeneralRe: floating point precision issue Pin
Stuart Dootson3-Feb-09 1:06
professionalStuart Dootson3-Feb-09 1:06 
AnswerRe: floating point precision issue Pin
CPallini3-Feb-09 0:00
mveCPallini3-Feb-09 0:00 
AnswerRe: floating point precision issue Pin
Stuart Dootson3-Feb-09 0:38
professionalStuart Dootson3-Feb-09 0:38 
AnswerRe: floating point precision issue Pin
David Crow3-Feb-09 4:47
David Crow3-Feb-09 4:47 
QuestionCompile time error for 'string.h' Pin
Supriya Tonape2-Feb-09 23:33
Supriya Tonape2-Feb-09 23:33 
AnswerRe: Compile time error for 'string.h' Pin
Cedric Moonen2-Feb-09 23:37
Cedric Moonen2-Feb-09 23:37 
GeneralRe: Compile time error for 'string.h' Pin
Supriya Tonape2-Feb-09 23:44
Supriya Tonape2-Feb-09 23:44 
AnswerRe: Compile time error for 'string.h' Pin
prasad_som3-Feb-09 3:27
prasad_som3-Feb-09 3:27 
QuestionRe: Compile time error for 'string.h' Pin
David Crow3-Feb-09 4:50
David Crow3-Feb-09 4:50 

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.