473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
22 2705
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

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
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
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
[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
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
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
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
>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
>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
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
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
<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
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

"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

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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
12
by: Anthony Jones | last post by:
Just a bit of background: I'm one of a group of FORTRAN programmers, looking to switch to C++. We are trying to write a few simple examples to demonstrate the power of the language to our manager,...
3
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
3
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
7
by: v4vijayakumar | last post by:
Is it possible to implement member object's virtual functions, in the containing class? If not, is it possible to simulate this behavior? ex: class test { protected: virtual void fun() = 0;...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
3
by: Ramesh | last post by:
Hi, I am trying to create an array of pointers to member functions inside my class. When I created a global array of type pfn & initialized with member functions and copy it back to the member...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.