invoking member functions without creating an object or pointer of the class?

A

Attila Feher

benben said:
The whole point of using member functions is that they are invoked with
an object. If you don't want that, don't use a (non-static) member
function. Use a standalone function or a static member function.

But if that's for debugging purpose, maybe you can use some pointer
trick. But I'm not sure how valid the program would go;

test* t = 0;
t->fun();

Nope. That is illegal. Don't ever do it!

WW aka Attila

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
A

Arvind Sharma

You can declare the member function as a 'static' function, then you don't
need the object to be instantiated.

In your example - if fun() is declared as 'static void fun()', then you can
call it as

test.fun()

Arvind


Is there any possibility of invoking the member functions of a class
without creating an object (or even a pointer to ) of that class.

eg.
#include <iostream.h>
class test
{
public:
void fun()
{
cout<<"Inside test::fun\n";
}
};

I want to call fun() of class test without creating
test t or even test * ptr;?
I searched on the net for the convincing answer but didn't get any.
(http://www.devx.com/tips/Tip/15846 )

Or is this a totally insane question? (I won't mind if anyone agrees
with that :)..)

Thanks and Regards,
Yogesh Joshi

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
M

Marcus Kwok

In comp.lang.c++ Arvind Sharma said:
You can declare the member function as a 'static' function, then you don't
need the object to be instantiated.

In your example - if fun() is declared as 'static void fun()', then you can
call it as

test.fun()

You mean:

test::fun()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top