Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C
Expertise: Intermediate
May 29, 2003

Access a Class Member Function Without Creating a Class Object

In some cases, it is possible to call a class member function without creating the class object.

In the following example, the program will print "hello world" although class A has never been created. When the program enters the "PrintMe" function, the "this" pointer is zero. This is fine as long as you don't access data members through the "this" pointer.


#include <stdio.h>
class A {
public:
	void PrintMe();
};


void A::PrintMe()
{
    printf("Hello World\n");
}

void main()
{

A* p = 0;
p-<PrintMe();

}
David Vengrover
 
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