473,796 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help in resolving issue: static member function of class

6 New Member
Hi,

i have a c++ class,

class XYZ
{
public:
void Function1 ( MyStruct * myStruct );
// member variables, etc
}


Note that, Function1 is non-static function. MyStruct is some typdef struct

now consider the following typdef.

typedef void MyFunction ( MyStruct * myStruct );

i have another function, (not part of any class). It takes a function pointer as its argument.

void Function2 ( MyFunction * pFn )
{
// body
}


Then, i go about creating an object of class XYZ, and pass the address of Function1 to it,

XYZ *object1 = new XYZ;
Function2 ( object1->Function1 );


This throws an error, cannot convert parameter 1 from 'void ( MyStruct *)' to 'MyFunction (__cdecl *)'
however, when i declareFunction 1 as static void Function1 ( MyStruct * ), the compiler compiles without any error.

Why is it that only a static member function is passed off without error and the non-static shows error?

I have tried type casting too,
Function2 ( (MyFunction*) object1->Function1 ); no avail.

I have also tried creating an object without the new operator, even then, no avail.

please help
Feb 10 '09 #1
3 2524
weaknessforcats
9,208 Recognized Expert Moderator Expert
Class member functions have an implied first argument that is the address of the object the member function is to use. That is, the this pointer is passed by the compiler implicitly.

Since static member functions do not apply to any particular object, they don't have this implied first argunent.

That's why static member functions work when passed by address and reguklar member functions do not.
Feb 10 '09 #2
Lakshmank85
6 New Member
how do i go about doing the same thing, without making it as non static? i tried the following:
class XYZ
{
public:
void Function1 ( MyStruct * myStruct );
void (*ptrFunction1) (Mystruct*);
// member variables, etc
}


//Constructor:
XYZ()
{
ptrFunction1 = &Function1; //error in this line
}


ERROR: '&' : illegal operation on bound member function expression

so i tried

//Constructor:
XYZ ()
{
this->ptrFunction1 = this->Function1; //error in this line
}



ERROR: XYZ::Function1' : forming a pointer-to-member requires explicit use of the address-of operator ('&') and a qualified name
ERROR: '=' : cannot convert from 'void (__thiscall XYZ::* )(MyStruct *)' to 'void (__cdecl *)(My_Struct *)'

When i use ampersand (&), and also without it, i get error.

Help, please
Feb 11 '09 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Again I say, XYZ::Function1 is a member function. It has two arguments.
The first is an XYZ* supplied by the compiler and the second is a MyStruct*.
You cannot assign the address of this function to a function pointer to a function
that has one argument.

Also, it is the compiler that supplies this implicit first argument. You cannot
do this yourself.

The usual method here is to use a static member function since it has no implied
arguments.

I do question why you need the function pointer in the first place. Are you saying
that a particular XYZ object is to call XYZ::Function1 using some other XYZ object?
If so, you replace the function pointer with an XYZ* (or XYZ&) and then call
XYZ::Function1 using the pointer or reference.
Feb 11 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
2253
by: Jeff | last post by:
Hello. Quick background as I dont wish to hog anyones time. I am a customer of a PHP/SQL programmer. A website that I had the programmer build for me is working great, with one exception. I have limited programming experience, most of it Pascal/C++ and I haven't programmed for many years now even then it was mediocre programming of small this and thats.
29
4413
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member reffers in all objects of this class to the same data Or in other word by using the static keyword all objects of one class can share data. (This is what I want)
30
3500
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
11
4619
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
3
2182
by: CoolPint | last post by:
I read that the return type has to be exactly same for a virtual function to be overriden. While testing something, I discovered something I cannot understand. I cannot understand why the code below compiles. I see that it won't compile if I have "virtual char & vtest() { }" in Base class and have "virtual const char & vtest() { }" in Derived class. But it compiles if I have "virtual const char & vtest() { }" in Base
15
6595
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
8
2042
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the constness of static members. Is there a way to do that? Also, is there a way for a static member function to guarantee constness of static members? TIA Regards,
7
3041
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class is a helper Singleton class, and is created & deleted as and when required) - Where does the static pointer point to when every single instance of the class is deleted. I presume it'll be dangling pointer- as the code seg to which it's
12
2433
by: StephQ | last post by:
I face the following problem. I wrote a poor's man plotting function: computePlot. This function append some x-values belonging to step and the correseponding f( x ) (for a given const member function f that takes and return a double) values to a reference stringstream, let's call this ss. Then I usually use ss.str() to transfer the results in a file and plot these results using an external software like gnuplot.
0
9683
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
10457
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...
1
10176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
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
9054
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
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
5443
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
4119
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

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.