Bhadresh
July 2nd, 1999, 04:12 AM
Hi,
I have one query, can you please explain it to me..
Below is an example,
Here, In derive class, I am making virtual function Private which is Public in Base class.
I am calling that private function by base class pointer.
As the function is private in derive class, that function should not called from main(). But compiler doesn't give any error.
I know that compiler cannot detect these errors..
My question is,
Is this a correct behavior in terms of C++ ? Calling private function from outside ?
or
This is a fault but because of the limitation of compiler we cannot detect it ?
or
something else ?
Can you please write me ASAP.
#include "stdafx.h"
// some pure virtual (abstract) class
class Base
{
public:
virtual CallMe() = 0;
};
// Some derived class
class Derive1 : public Base
{
public:
CallMe(){ AfxMessageBox( " Derive1");}
};
// Some derived class
class Derive2 : public Base
{
private:
CallMe(){ AfxMessageBox( " Derive2");
};
void main()
{
Derive1 d1;
Derive2 d2;
Base* b;
d1.CallMe(); // NO ERROR
b = (Base*) &d2;
b->CallMe(); // A BIG FLAW I CAN CALL PRIVATE MEMBER OF Derive2
}
Bhadresh,
Mahindra-British telecom, Pune, India
I have one query, can you please explain it to me..
Below is an example,
Here, In derive class, I am making virtual function Private which is Public in Base class.
I am calling that private function by base class pointer.
As the function is private in derive class, that function should not called from main(). But compiler doesn't give any error.
I know that compiler cannot detect these errors..
My question is,
Is this a correct behavior in terms of C++ ? Calling private function from outside ?
or
This is a fault but because of the limitation of compiler we cannot detect it ?
or
something else ?
Can you please write me ASAP.
#include "stdafx.h"
// some pure virtual (abstract) class
class Base
{
public:
virtual CallMe() = 0;
};
// Some derived class
class Derive1 : public Base
{
public:
CallMe(){ AfxMessageBox( " Derive1");}
};
// Some derived class
class Derive2 : public Base
{
private:
CallMe(){ AfxMessageBox( " Derive2");
};
void main()
{
Derive1 d1;
Derive2 d2;
Base* b;
d1.CallMe(); // NO ERROR
b = (Base*) &d2;
b->CallMe(); // A BIG FLAW I CAN CALL PRIVATE MEMBER OF Derive2
}
Bhadresh,
Mahindra-British telecom, Pune, India