dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Intermediate
Aug 16, 2005

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Call a Private Method of a Class

Use this polymorphic technique to cheat the compiler and expose private information:

See the code below.
class A{
public:
	virtual void f2()
	{
		printf("in A::f2\n");
	}
};

class B: public A{
private:
	void f2(int j)
	{
		printf("in B::f2\n");
	}	
};

int main()
{
	B b;
	A *a = &b
	a->f2(10);
	
}
Because a->f2 is virtual and called run time, using it calls f2 of B. F2 is private in B, but still you're still able to call it. That's the magic.
Ganesh Kumar
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date