473,385 Members | 1,647 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,385 software developers and data experts.

Ambiguous member function pointer

I have a class that has two member functions of the same name:

class MyClass {
public:
void someFunction(int arg);
void someFunction(int arg, char blah);
};

I'm trying to get a tr1::functional to the first one, for example:

std::tr1::function<void (MyClass*, int)func_obj;
func_obj = &Myclass::someFunction;

But compilation fails on the second line, since MyClass::someFunction
is ambiguous.

The only way around it I've found is to first create a member function
pointer, then pass that to the constructor of the function object.

I'm curious if there's a better way to do it, or if I'm stuck with
using function pointers as intermediates? I'm using linux gcc 4.1.

Thanks,
Caleb

Nov 9 '06 #1
6 4118
Caleb wrote:
I have a class that has two member functions of the same name:

class MyClass {
public:
void someFunction(int arg);
void someFunction(int arg, char blah);
};

I'm trying to get a tr1::functional to the first one, for example:

std::tr1::function<void (MyClass*, int)func_obj;
func_obj = &Myclass::someFunction;

But compilation fails on the second line, since MyClass::someFunction
is ambiguous.

The only way around it I've found is to first create a member function
pointer, then pass that to the constructor of the function object.

I'm curious if there's a better way to do it, or if I'm stuck with
using function pointers as intermediates? I'm using linux gcc 4.1.
I am not very proficient with TR1 yet, so educate me, if you will.
Does the use of 'std::tr1::function' suddenly allow conversion between
a pointer-to-member and a pointer-to-function (disallowed by the C++
language proper definition)?

void (MyClass::*)(int)

and

void (*)(MyClass*, int)

are not the same type. Does 'std::tr1::function' somehow overcome
the problem of those types' incompatibility?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 9 '06 #2
Caleb wrote:
I have a class that has two member functions of the same name:

class MyClass {
public:
void someFunction(int arg);
void someFunction(int arg, char blah);
};

I'm trying to get a tr1::functional to the first one, for example:

std::tr1::function<void (MyClass*, int)func_obj;
func_obj = &Myclass::someFunction;
typedef void (MyClass::*fptr)(int);

func_obj = (fptr)&MyClass::someFunction;

You can also do it without the typedef, but it's a bit more confusing.

--

-- Pete
Roundhouse Consulting, Ltd. -- www.versatilecoding.com
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Nov 9 '06 #3
I am not very proficient with TR1 yet, so educate me, if you will.
Does the use of 'std::tr1::function' suddenly allow conversion between
a pointer-to-member and a pointer-to-function (disallowed by the C++
language proper definition)?
It allows it in the sense that you can do this:

class MyClass {
void someFunction(int)
};

std::tr1::function<int (MyClass*, int)func;
func = &MyClass::someFunction;

MyClass mc;
func(&mc, 10);

I assume there's some magic going on behind the scenes. But my issue
is that if MyClass::someFunction is ambiguous, it won't compile. I'm
looking for a way to tell the compiler which someFunction to use.

Nov 9 '06 #4
typedef void (MyClass::*fptr)(int);
>
func_obj = (fptr)&MyClass::someFunction;

You can also do it without the typedef, but it's a bit more confusing.
Thanks, Pete. This is what I was looking for.

Caleb

Nov 9 '06 #5
Victor Bazarov wrote:
>
I am not very proficient with TR1 yet, so educate me, if you will.
Does the use of 'std::tr1::function' suddenly allow conversion between
a pointer-to-member and a pointer-to-function (disallowed by the C++
language proper definition)?

void (MyClass::*)(int)

and

void (*)(MyClass*, int)

are not the same type. Does 'std::tr1::function' somehow overcome
the problem of those types' incompatibility?
Yup, as do std::tr1::mem_fn, std::tr1::bind, and
std::tr1::reference_wrapper. You can wrap a pointer to member function
inside any of them, and you get a callable object whose function call
operator takes an argument that's a pointer or reference to an object,
and calls the member function on that object. In particular,
std::tr1::function<void(MyClass*, int)defines a type with a function
call operator that takes two arguments, the first of type MyClass* and
the second of type int, and returns void.

struct S // sorry, MyClass is too long to type.
{
void f(int);
};

void g(S*, int);

S *sp = new S;
std::tr1::function<S*, intfun = g;
fun(s, 3); // calls g(s, 3)
s = &S::f; // yes, this assignment is intentional
fun(s, 3); // calls (s->*f)(3);

There's actually quite a bit more you can do, like wrap a pointer to
member data (creating a type with a function call operator that takes
exactly one argument), and you can call member functions with objects,
references to objects, pointers to objects, and smart pointers to
objects. For a more complete description, see sections 6.1 and 6.2 of my
book, "The C++ Standard Library Extensions."

--

-- Pete
Roundhouse Consulting, Ltd. -- www.versatilecoding.com
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Nov 9 '06 #6
Victor Bazarov wrote:
...
Does the use of 'std::tr1::function' suddenly allow conversion between
a pointer-to-member and a pointer-to-function (disallowed by the C++
language proper definition)?

void (MyClass::*)(int)

and

void (*)(MyClass*, int)

are not the same type. Does 'std::tr1::function' somehow overcome
the problem of those types' incompatibility?
...
Take a look at

http://www.ddj.com/184406110

and, in particular, listing four

http://www.ddj.com/184406110#l4

Note, that it doesn't deal with the ever-so-popular problem of converting 'void
(MyClass::*)(int)' to 'void (*)(int)', i.e. it does not implement the so called
"closure" functionality. It simply converts the implicit 'this' parameter into
and explicit one. The latter is not that difficult to achieve with a simple wrapper.

--
Best regards,
Andrey Tarasevich
Nov 9 '06 #7

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

Similar topics

3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
4
by: Chameleon | last post by:
Can anyone explain me why the code below produce (with mingw-g++) the following error message: --------------------------------------------------------------- main10.cpp: In function `int main()':...
2
by: pvl_google | last post by:
Hi, I'm trying to extend an STL class with additional iterator functionality. In the simplified example below I added an extra iterator class with a dereferencing operator. This operator...
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: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
9
by: neildferguson | last post by:
I am using templates with a little project I am working on. My compiler (GCC) is finding a particular construct ambiguous. Can anyone suggest something I might change in the declaration of class...
0
by: Ioannis Vranos | last post by:
Although not about C++ only, I think many people here have K&R2, so I post this message in clc++ too. In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html>...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.