473,837 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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++.m oderated. First time posters: Do this! ]

Jan 16 '06 #1
22 2787
yp*********@ind iatimes.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 misunderstandin g
of the purpose of non-static member functions).

V
Jan 16 '06 #2

yp*********@ind iatimes.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++.m oderated. First time posters: Do this! ]

Jan 16 '06 #3
On 16 Jan 2006 09:14:36 -0500, yp*********@ind iatimes.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++.m oderated. First time posters: Do this! ]

Jan 16 '06 #4
In comp.lang.c++ yp*********@ind iatimes.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++.m oderated. 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.goo glegroups.com.. .

yp*********@ind iatimes.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.goo glegroups.com.. .
yp*********@i ndiatimes.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++.m oderated. 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++.m oderated. 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
9384
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, 2, func);
12
2096
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, so he will send us all on a conversion course. One of many reasons is that our code is littered with examples of: SUBROUTINE PRINT_ITEM(ITEM, ITEM_TYPE) IF (ITEM_TYPE .EQ. SQUARE) THEN CALL PRINT_SQUARE(ITEM)
3
2002
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 in only objects of class B and skip over the objects of class C. To complicate things, I'm using the vector position (index) as an argument in the invoked member function. It is possible to move the position into the object by adding a data...
37
5038
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 signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
3
2256
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
1791
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
2539
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 functions in common implementations? And where is the 'this' ptr tucked away at for POD structs with member functions? John
7
3977
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 functions. Is it normal how C++ Compiler can compile large class without any problem? Didn't C++ Compiler have rules to limit the number of member functions? One big object has complex operations how member variables and member functions can be...
3
2829
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 function pointer array (Handlers) - compiler doesnt let me do that. I got an error as below: "invalid use of non-static member function bool test::func1(long int)"
0
9827
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9678
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10263
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6987
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5663
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4468
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4034
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3120
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.