By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,582 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

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

P: n/a
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! ]

Jan 16 '06 #1
Share this Question
Share on Google+
22 Replies


P: n/a
yp*********@indiatimes.com wrote:
Is there any possibility of invoking the member functions of a class
without creating an object (or even a pointer to ) of that class.
No, unless it's a static member.

eg.
#include <iostream.h>
No such standard header.
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;?
Since your 'fun' function does not need an object, make it 'static'.
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 :)..)


It's not. But it seems to stem from a poor design (or misunderstanding
of the purpose of non-static member functions).

V
Jan 16 '06 #2

P: n/a

yp*********@indiatimes.com wrote:
Is there any possibility of invoking the member functions of a class
without creating an object (or even a pointer to ) of that class.
[]
Or is this a totally insane question? (I won't mind if anyone agrees
with that :)..)


It seems like one.

A member function of a properly designed class accesses object's data
members. If it does not, then the function should not be a member. So,
if you don't have the data the function operates upon, where is the
function is supposed to find the data?

Bear in mind, that a member function is a function with a hidded
argument that has name 'this' inside the function.

Given the declaration:

struct A { void foo(); };

A::foo() is essentially

void foo(A* const this);

And you can hack the language and call it without a valid object like
this:

((A*)0)->foo();

But once the function accesses data pointed to by 'this' pointer you
are in troubles.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jan 16 '06 #3

P: n/a
On 16 Jan 2006 09:14:36 -0500, yp*********@indiatimes.com wrote:
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


fun must be a static member function.

static void fun()
{
std::cout << "inside test::fun\n";
}

As the method doesn't use the this pointer at all, it probably should
be static anyway.

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

Jan 16 '06 #4

P: n/a
In comp.lang.c++ yp*********@indiatimes.com wrote:
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 )


Look at static member functions.

--
Marcus Kwok

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

Jan 16 '06 #5

P: n/a
[re-posting after apparent send failure]

"Maxim Yegorushkin" <ma***************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

yp*********@indiatimes.com wrote:
Is there any possibility of invoking the member functions of a class
without creating an object (or even a pointer to ) of that class.
[]
Or is this a totally insane question? (I won't mind if anyone agrees
with that :)..)


It seems like one.


Why?
A member function of a properly designed class accesses object's data
members. If it does not, then the function should not be a member. So,
if you don't have the data the function operates upon, where is the
function is supposed to find the data?


What about static members? Are you suggesting that they serve no purpose?
(And, they CAN access static member variables, just not non-static ones.)

-Howard


Jan 16 '06 #6

P: n/a
Howard wrote:
[re-posting after apparent send failure]

"Maxim Yegorushkin" <ma***************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
yp*********@indiatimes.com wrote:
Is there any possibility of invoking the member functions of a class
without creating an object (or even a pointer to ) of that class.


[]

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


It seems like one.

Why?

A member function of a properly designed class accesses object's data
members. If it does not, then the function should not be a member. So,
if you don't have the data the function operates upon, where is the
function is supposed to find the data?

What about static members? Are you suggesting that they serve no purpose?
(And, they CAN access static member variables, just not non-static ones.)


The CAN access non-static members for any instance of the class, provided
they have a pointer or a reference to that instance, or they create one
internally.

V
Jan 16 '06 #7

P: n/a
bob
class test
{
public:
static void fun()
{
cout<<"Inside test::fun\n";
}

};
then call test::fun() from main.

there's things to be aware of with static functions so you might want
to read up on them if you haven't used them before.
BTW no such thing as an insane question on this forum... ;)
hope this helps

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

Jan 16 '06 #8

P: n/a
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();

Ben

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

Jan 16 '06 #9

P: n/a
>The CAN access non-static members for any instance of the class, provided
they have a pointer or a reference to that instance, or they create one
internally. V


They can access private non-static data members? is this class valid
then:
class A {
private:
int x;
public:
static void blah(A* this)
{
//assuming x is initialized
std::cout<<this->x<<std::endl;
}

};

Jan 16 '06 #10

P: n/a
>The CAN access non-static members for any instance of the class, provided
they have a pointer or a reference to that instance, or they create one
internally.
V


Ok, apologies! The example above will not work, but this will work:

class A {
private:
int x;
public:
A()
:x(10)
{
}
static void blah()
{
A* u;
cout<<u->x<<endl;
}
};

Jan 16 '06 #11

P: n/a
Shark wrote:
The CAN access non-static members for any instance of the class, provided
they have a pointer or a reference to that instance, or they create one
internally.
V

They can access private non-static data members? is this class valid
then:
class A {
private:
int x;
public:
static void blah(A* this)


No. 'this' is a keyword, it cannot be used here. Rewrite it and use
'that', for example.
{
//assuming x is initialized
std::cout<<this->x<<std::endl;
Same thing. 'this' is a keyword. Use the actual name of the argument
once you've corrected it there.
}

};


Other than the errors I pointed out, yes, the code is well-formed.

V
Jan 16 '06 #12

P: n/a
Shark wrote:
The CAN access non-static members for any instance of the class, provided
they have a pointer or a reference to that instance, or they create one
internally.
V

Ok, apologies! The example above will not work, but this will work:

class A {
private:
int x;
public:
A()
:x(10)
{
}
static void blah()
{
A* u;
cout<<u->x<<endl;


No. This program has undefined behaviour because it dereferences
an uninitialised pointer. What would work is this:

A u;
std::cout << u.x << std::endl;

(provided proper includes have been made).
}
};


V
Jan 16 '06 #13

P: n/a
<yp*********@indiatimes.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Is there any possibility of invoking the member functions of a class
without creating an object (or even a pointer to ) of that class.


Sure -- just make the member function static. Static member functions are
not permitted to use the "this" pointer, which means that you don't need an
instance of the object to invoke one.

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

Jan 16 '06 #14

P: n/a
yp*********@indiatimes.com wrote:
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 )
I think you are looking for a static member function. Get your favorite
C++ texbook, and read about them. Those kind of functions are member
functions of a class, but they do not need an object to work on - and
they do not get an object to work on (unless you pass it as a named
argument). To simply put: static member functions see and have no
"this" pointer.

#include <iostream> // iostream.h is not standard

class test {
public:
static void fun() {
std::cout<<"Inside test::fun\n";
}
};

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


Ehem... If you want to call fun() as a function that works on an
instance of that class but you don't want to tell which, then it is
pretty much hopeless. :-)

WW aka Attila

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

Jan 16 '06 #15

P: n/a

"Maxim Yegorushkin" <ma***************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

yp*********@indiatimes.com wrote:
Is there any possibility of invoking the member functions of a class
without creating an object (or even a pointer to ) of that class.
[]
Or is this a totally insane question? (I won't mind if anyone agrees
with that :)..)


It seems like one.


Why?
A member function of a properly designed class accesses object's data
members. If it does not, then the function should not be a member. So,
if you don't have the data the function operates upon, where is the
function is supposed to find the data?


What about static members? Are you suggesting that they serve no purpose?
(And, they CAN access static member variables, just not non-static ones.)

-Howard

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

Jan 16 '06 #16

P: n/a

Howard wrote:
[re-posting after apparent send failure]

"Maxim Yegorushkin" <ma***************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

yp*********@indiatimes.com wrote:
Is there any possibility of invoking the member functions of a class
without creating an object (or even a pointer to ) of that class.
[]
Or is this a totally insane question? (I won't mind if anyone agrees
with that :)..)


It seems like one.

Why?


The following elaborates that.
A member function of a properly designed class accesses object's data
members. If it does not, then the function should not be a member. So,
if you don't have the data the function operates upon, where is the
function is supposed to find the data?


What about static members?


The language supports them.
Are you suggesting that they serve no purpose?
I do not. I wrote here about nonstatic member functions, rather than
static member functions, because OP did not ask about static ones.
(And, they CAN access static member variables, just not non-static ones.)


They can access any member variables, including non static, as Victor
has already noted.

Jan 17 '06 #17

P: n/a
Maxim Yegorushkin wrote:
Bear in mind, that a member function is a function with a hidded
argument that has name 'this' inside the function.

Given the declaration:

struct A { void foo(); };

A::foo() is essentially

void foo(A* const this);
With the fundamental difference that in order to call the
member function A::foo(), you need an object of class type
A, so if you call foo() with a pointer to A, the latter is
dereferenced;
pA->foo();
is equivalent to
(*pA).foo();
And you can hack the language and call it without a valid object like
this:

((A*)0)->foo();

But once the function accesses data pointed to by 'this' pointer you
are in troubles.


Or when foo() happens to be virtual, or when you use some
exotic compiler / compiler options, or when you compile the
program after midnight at Full Moon - that's what Undefined
Behaviour is about.

By the way, even a test
if(this == 0)
in the body of foo() may be optimised away by the compiler,
or may fail to detect that foo() was called on a NULL pointer
- especially when called on a pointer to a derived class where
pointers might need adjustment due to multiple inheritance.

Falk

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

Jan 17 '06 #18

P: n/a
yp*********@indiatimes.com wrote:
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>
Since someone else pointed out that this header is out of date:

The standard conformant solution (and the one you should
definitly prefer unless you are stuck with millions of lines of
legacy code) is

#include <iostream>
#include <ostream>

and to prepend all references to objects and functions in the
standard library with std::, e.g. std::cout, instead of cout.

Both headers are necessary.
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.
As others have pointed out, the answer is to declare the
function static.
(http://www.devx.com/tips/Tip/15846 )


The example here is totally illegal. If this is the sort of
thing posted at this site, avoid it like the plague. It's just
plain wrong.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jan 17 '06 #19

P: n/a

"Maxim Yegorushkin" <ma***************@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

Howard wrote:
[re-posting after apparent send failure]

"Maxim Yegorushkin" <ma***************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
>
> yp*********@indiatimes.com wrote:
>> Is there any possibility of invoking the member functions of a class
>> without creating an object (or even a pointer to ) of that class.
>
> []
>
>> Or is this a totally insane question? (I won't mind if anyone agrees
>> with that :)..)
>
> It seems like one.
> Why?


The following elaborates that.


I see nothing below that elaborates why the question is "insane".
What about static members?


The language supports them.


And they are how to accomplish what the OP asked.
Are you suggesting that they serve no purpose?
I do not. I wrote here about nonstatic member functions, rather than
static member functions, because OP did not ask about static ones.


He asked for a solution. Static member functions _are_ a solution, to
_exactly_ this kind of problem. (If he knew about static members, then why
ask the question?)
(And, they CAN access static member variables, just not non-static ones.)


They can access any member variables, including non static, as Victor
has already noted.


I know that. I never suggested a static function could _not_ access
non-static data. (Although they need to refer to a specific object, which
is what the OP didn't want.) I was merely arguing against your point that
the design must be wrong. I'll repeat a portion of your answer. You said:
> A member function of a properly designed class accesses object's data
> members. If it does not, then the function should not be a member. So,
> if you don't have the data the function operates upon, where is the
> function is supposed to find the data?


You suggest you don't "have the data", and ask where the function is
supposed to find the data. My point was that static members can still
access static members, even if they can't access non-static members, at
least without referring to some specific object (which is what the OP was
asking about).

Also, everything in that paragraph suggests that a "proper design" would
make a function either a (non-static) member or a non-member. I was
pointing out (as much to the OP as to you), that static functions are a
perfectly valid solution. And if you'll look at all the other posts, they
say the same thing I did.

Seems to me the question isn't "insane" at all.

-Howard


Jan 17 '06 #20

P: n/a
benben wrote:
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! ]

Jan 18 '06 #21

P: n/a
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
<yp*********@indiatimes.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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! ]

Mar 22 '06 #22

P: n/a
In comp.lang.c++ Arvind Sharma <ar****@nospamlucent.com> wrote:
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()

--
Marcus Kwok

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

Mar 23 '06 #23

This discussion thread is closed

Replies have been disabled for this discussion.