473,756 Members | 3,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function pointers and wrapper classes

Hi,

I'm curious to the syntax of calling member functions through pointers
of classes returned through the -operator. For example (excuse the
crude incomplete code);

Here are the classes:

Class Foo{

public:
doStuff();
};

Class FooRef{
Foo foo;
int refCount = 0;
public:
Foo& operator::->{refCount++;re turn foo;}
};

now here is my function

callStuff( FooRef& myFooRef, void (Foo::*doStuff) () )
{
myFooRef->doStuff(); // compiler error
myFooRef->*doStuff(); // compiler error
}

finally, here is the main:

main()
{
FooRef foor;
callStuff(foor, &Foo::doStuf f);
}

I'm not sure what the correct syntax is to get this going inside the
callStuff function. Maybe I'm just forgetting something and doing this
is illegal.

thanks

Sep 20 '06 #1
5 2337
mancomb schrieb:
Hi,
Hi!
I'm curious to the syntax of calling member functions through pointers
of classes returned through the -operator.
Note: in the examples below you never returned any pointer to a class,
only references.
For example (excuse the
crude incomplete code);
No comment.
Here are the classes:

Class Foo{

public:
doStuff();
};

Class FooRef{
Foo foo;
int refCount = 0;
public:
Foo& operator::->{refCount++;re turn foo;}
};
First: Syntax errors:
* consider copy and paste instead of writing code
in your newsgroup app
* class, not Class
* operator->(), not operator::->
* int refCount = 0; won't work. Initialize it in the c_tor.

Second:
* Do you really want/need a class FooRef? You probably want a
so-called smart pointer. I would suggest boost::shared_p tr, very easy,
very good. See boost.org. (Note that for boost specific questions there
exists a mailing list, also available via GMANE)
now here is my function

callStuff( FooRef& myFooRef, void (Foo::*doStuff) () )
{
myFooRef->doStuff(); // compiler error
myFooRef->*doStuff(); // compiler error
}
Well, actually the compiler error should happen when you write
'Class Foo', but that's not the point. And you must specify a return
type for callStuff. The parameter doStuff does not make any sense, as I
will explain below.
finally, here is the main:

main()
{
FooRef foor;
callStuff(foor, &Foo::doStuf f);
}
What you meant is probably something like this:

int main()
{
FooRef foor;
foor->doStuff(); // that's it, nothing more.
return 0;
}

What you want is maybe this:

#include <boost/shared_ptr.hpp>
/// omitted class definition
typedef boost::shared_p tr<FooFooPtr;
int main()
{
FooPtr foor(new Foo());
foor->doStuff();
return 0;
}
I'm not sure what the correct syntax is to get this going inside the
callStuff function. Maybe I'm just forgetting something and doing this
is illegal.
The way you wrote it's more than just illegal, but I hope I guessed
right when I wrote what you actually want.
Best regards,
-- Markus
thanks
Sep 20 '06 #2
Markus,

Thanks for your reply. The code in my original post was just pseudo
code intended to get some ideas of what I'm trying to do across. I
never expected that beast to compile anywhere but a parallel universe.
Yes the classes I'm dealing with are the shared pointers you mention.
Maybe I can use your clearer code to rephrase my question.

int main()
{
FooPtr foor(new Foo());
foor->doStuff();
return 0;
}

I want to pass my FooPtr into a function along with a pointer to the
function Foo::doStuff(). Then I would like to dereference my FooPtr
with the arrow operator and call the function doStuff through the
function pointer. If no FooPtr's were involved it would look like this:

// the function

void myFunc( Foo& myFoo, void Foo::*fooFunc() )
{
myFoo.*fooFunc( );
}

// main:

int main()
{
Foo myFoo;
// assume Foo class has functions void doStuff(); and void
doMoreStuff();

myFunc(myFoo, &Foo::doStuf f);
myFunc(myFoo, &Foo::doMoreStu ff);
}

So I'd like myFunc to take in a FooPtr instead of just a Foo and still
keep it's functionality. Am I making sense yet or is it getting worse?

regards

Sep 20 '06 #3
mancomb wrote:
Thanks for your reply. The code in my original post was just pseudo
code intended to get some ideas of what I'm trying to do across. I
never expected that beast to compile anywhere but a parallel universe.
That's nice, but see the FAQ on the polite way to post code that you
have a question about:

http://parashift.com/c++-faq-lite/ho...t.html#faq-5.8

In short, work up a minimal but complete (or as complete as possible)
example that we can paste unchanged into our editors to see the
problem.
Yes the classes I'm dealing with are the shared pointers you mention.
Maybe I can use your clearer code to rephrase my question.

int main()
{
FooPtr foor(new Foo());
foor->doStuff();
return 0;
}

I want to pass my FooPtr into a function along with a pointer to the
function Foo::doStuff(). Then I would like to dereference my FooPtr
with the arrow operator and call the function doStuff through the
function pointer. If no FooPtr's were involved it would look like this:

// the function

void myFunc( Foo& myFoo, void Foo::*fooFunc() )
{
myFoo.*fooFunc( );
}

// main:

int main()
{
Foo myFoo;
// assume Foo class has functions void doStuff(); and void
doMoreStuff();

myFunc(myFoo, &Foo::doStuf f);
myFunc(myFoo, &Foo::doMoreStu ff);
}

So I'd like myFunc to take in a FooPtr instead of just a Foo and still
keep it's functionality. Am I making sense yet or is it getting worse?
The FAQ answers your questions, methinks:

http://www.parashift.com/c++-faq-lit...o-members.html

Cheers! --M

Sep 20 '06 #4
mancomb schrieb:
Markus,

Thanks for your reply. The code in my original post was just pseudo
code intended to get some ideas of what I'm trying to do across. I
never expected that beast to compile anywhere but a parallel universe.
Please try to write minimal compiling samples for illustration. You'll
also get more answers faster and friendlier responses.
Yes the classes I'm dealing with are the shared pointers you mention.
Maybe I can use your clearer code to rephrase my question.

int main()
{
FooPtr foor(new Foo());
foor->doStuff();
return 0;
}

I want to pass my FooPtr into a function along with a pointer to the
function Foo::doStuff(). Then I would like to dereference my FooPtr
with the arrow operator and call the function doStuff through the
function pointer. If no FooPtr's were involved it would look like this:

// the function

void myFunc( Foo& myFoo, void Foo::*fooFunc() )
{
myFoo.*fooFunc( );
}

// main:

int main()
{
Foo myFoo;
// assume Foo class has functions void doStuff(); and void
doMoreStuff();

myFunc(myFoo, &Foo::doStuf f);
myFunc(myFoo, &Foo::doMoreStu ff);
}

So I'd like myFunc to take in a FooPtr instead of just a Foo and still
keep it's functionality. Am I making sense yet or is it getting worse?
Both ;-) My codesample is already the solution to what you want to do,
really. You don't need a function 'myFunc'.

Assuming:
//------------------------------------------
class foo {
public:
void doStuff();
void doMoreStuff();
}

typedef boost::shared_p tr<foofoo_ptr;
//------------------------------------------
Then you can call the methods doStuff() and doMoreStuff() via
derefencing a foo_ptr object:
//------------------------------------------
int main()
{
foo_ptr bar(new foo());
bar->doStuff(); // calls foo::doStuff with bar as this
bar->doMoreStuff( );
return 0;
}
//------------------------------------------

You really do not need to write a call_stuff_thro ugh_foo_ptr() method;
to describe what happens behind the curtains:

Let's do:

foo* bar= new foo();

Now you could:

(*bar).doStuff( );

Also written as:

bar->doStuff();

The operator-is nothing special, here it is just syntactical sugar,
known from ancient C times. I am sure you know that you can call
methods this way. But what happens?

*bar

bar is of type 'pointer to foo', and variables of type 'pointer to ...'
can be dereferenced via the * operator; the result of applying the
dereference operator to a pointer is a ... (smile!) reference, in this
case, type 'reference to foo', also written as

foo&

Now, we are better, we use smart pointers. How they work is of no
interest to us in this stage. We just need to know that smart pointers
behave in almost all important cases like a raw pointer. This means
(with bar of type smart_ptr<foo>) :

*bar

will be of type 'reference to foo'. Just like using a raw pointer. And

bar->doStuff()

will just call foo's member method doStuff(), applied on instance bar.
But maybe you meant applying non-member functions, which are only known
by signature and pointer, to an object. In this case, I recommend to
search the last week of this group for std::ostream and std::endl, where
an example of how to overload your operators to enable function
invocation is given.
best regards,
-- Markus
regards
Sep 20 '06 #5
mancomb wrote:
Markus,

Thanks for your reply. The code in my original post was just pseudo
code intended to get some ideas of what I'm trying to do across. I
never expected that beast to compile anywhere but a parallel universe.
Yes the classes I'm dealing with are the shared pointers you mention.
Maybe I can use your clearer code to rephrase my question.

int main()
{
FooPtr foor(new Foo());
foor->doStuff();
return 0;
}

I want to pass my FooPtr into a function along with a pointer to the
function Foo::doStuff(). Then I would like to dereference my FooPtr
with the arrow operator and call the function doStuff through the
function pointer. If no FooPtr's were involved it would look like this:

// the function

void myFunc( Foo& myFoo, void Foo::*fooFunc() )
{
myFoo.*fooFunc( );
}

// main:

int main()
{
Foo myFoo;
// assume Foo class has functions void doStuff(); and void
doMoreStuff();

myFunc(myFoo, &Foo::doStuf f);
myFunc(myFoo, &Foo::doMoreStu ff);
}
Why this and not just

myFoo.doStuff()
myFoo.doMoreStu ff()
>
So I'd like myFunc to take in a FooPtr instead of just a Foo and still
keep it's functionality. Am I making sense yet or is it getting worse?

regards
Sep 20 '06 #6

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

Similar topics

2
9600
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A, while an instance of class C should be able to check all methods #defined in C, B and A. #------------------------------------------------
37
5015
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.
2
2684
by: Chris Morley | last post by:
Hi, I have always done my C++ class callbacks with the age old 'using this pointer in parameter of the class's static callback function' and typecasting it to get the specific instance. However I'm left wondering are there any better ways of doing this? Just before I left work today I came across functors, which look promising. I will have a proper look tomorrow, but I have a few questions in the mean time.
4
1841
by: Alex Vinokur | last post by:
Hi, I need something like "function inheritance". typedef void (*func_type1) (int); typedef int (*func_type2) (double); typedef char (*func_type3) (short, int); .... I need a vector contains pointers to functions of types above.
27
3410
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined as ff: typedef struct { float *data ; int count ;
11
2295
by: Enquiries, Hopkins Research | last post by:
Hi all I have a conundrum that is puzzling me. I have a large codebase in C that I am converting to C++ as fast as possible (i.e. slowly because I keep learning new idioms and stumbling with C++ 'features'). One part of the C code is some optimisation functions that expect a pointer to a function which is sent an array of doubles and returns a double i.e. simplified..
10
7312
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
3
1825
by: Randy Yates | last post by:
Hi, We know we can build arrays of variables of the same type and arrays of functions of the same "type" (i.e., same return value and same parameters), but is there a way to automate the calling of a sequence of functions with arbitrary return types and/or parameters? -- Randy Yates Sony Ericsson Mobile Communications Research Triangle Park, NC, USA
92
5116
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers, but I just don't know. I don't like them because I don't trust them. I use new and delete on pure pointers instead. Do you use smart pointers?
0
9456
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
9275
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
10034
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
8713
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...
0
6534
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
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
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
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
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.