473,883 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ambiguous member function pointer

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

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

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

std::tr1::funct ion<void (MyClass*, int)func_obj;
func_obj = &Myclass::someF unction;

But compilation fails on the second line, since MyClass::someFu nction
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 4164
Caleb wrote:
I have a class that has two member functions of the same name:

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

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

std::tr1::funct ion<void (MyClass*, int)func_obj;
func_obj = &Myclass::someF unction;

But compilation fails on the second line, since MyClass::someFu nction
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::func tion' suddenly allow conversion between
a pointer-to-member and a pointer-to-function (disallowed by the C++
language proper definition)?

void (MyClass::*)(in t)

and

void (*)(MyClass*, int)

are not the same type. Does 'std::tr1::func tion' 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(in t arg);
void someFunction(in t arg, char blah);
};

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

std::tr1::funct ion<void (MyClass*, int)func_obj;
func_obj = &Myclass::someF unction;
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::func tion' 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(in t)
};

std::tr1::funct ion<int (MyClass*, int)func;
func = &MyClass::someF unction;

MyClass mc;
func(&mc, 10);

I assume there's some magic going on behind the scenes. But my issue
is that if MyClass::someFu nction 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::func tion' suddenly allow conversion between
a pointer-to-member and a pointer-to-function (disallowed by the C++
language proper definition)?

void (MyClass::*)(in t)

and

void (*)(MyClass*, int)

are not the same type. Does 'std::tr1::func tion' somehow overcome
the problem of those types' incompatibility ?
Yup, as do std::tr1::mem_f n, std::tr1::bind, and
std::tr1::refer ence_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::funct ion<void(MyClas s*, 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::funct ion<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::func tion' suddenly allow conversion between
a pointer-to-member and a pointer-to-function (disallowed by the C++
language proper definition)?

void (MyClass::*)(in t)

and

void (*)(MyClass*, int)

are not the same type. Does 'std::tr1::func tion' 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::*)(in t)' 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
12127
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 member function?
2
3645
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 support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
4
2258
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()': main10.cpp:23: error: request for member `getZero' is ambiguous main10.cpp:12: error: candidates are: virtual int B::getZero(int) main10.cpp:6: error: virtual int A::getZero()...
2
2467
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 internally relies on the at function of the superclass. #include <vector>
13
2543
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
3822
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
2207
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 Length so that I can use operator+ the way I'd like? //========================================= // File lentest.h: #ifndef FDIMENS_LENGTH_INCL #define FDIMENS_LENGTH_INCL
0
1831
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> there are some ambiguous errata, for which I propose solutions. Any comments are welcome.
5
4672
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
9778
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
11112
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
10405
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
9559
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
7114
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
5980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4602
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
4205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3228
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.