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

Y

ypjofficial

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! ]
 
V

Victor Bazarov

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
 
M

Marcus Kwok

In said:
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.
 
W

W Marsh

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! ]
 
M

Maxim Yegorushkin

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! ]
 
H

Howard

[re-posting after apparent send failure]

Maxim Yegorushkin said:
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
 
V

Victor Bazarov

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

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
 
B

benben

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! ]
 
B

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! ]
 
S

Shark

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.

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;
}

};
 
S

Shark

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;
}
};
 
V

Victor Bazarov

Shark said:
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
 
V

Victor Bazarov

Shark said:
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
 
A

Attila Feher

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! ]
 
A

Andrew Koenig

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! ]
 
H

Howard

Maxim Yegorushkin said:
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! ]
 
M

Maxim Yegorushkin

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

Maxim Yegorushkin said:
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.
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.
 
?

=?ISO-8859-1?Q?Falk_Tannh=E4user?=

Maxim said:
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! ]
 
K

kanze

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.

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! ]
 
H

Howard

Maxim Yegorushkin said:
[re-posting after apparent send failure]

Maxim Yegorushkin said:
(e-mail address removed) 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".
The language supports them.

And they are how to accomplish what the OP asked.
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?)
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:

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
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top