reference vs. int arg confusion

G

Guest

The following C++ program compiles without warning message
under RedHat 9.4 with a recent version of g++:

// all put in file main.cpp for simplicity
#include <stdio.h>
class MyClass
{
public:
void func (int);
void func (int&);
}

void MyClass::func (int x) { printf ("arg: %d\n", x); }
void MyClass::func (int x&) { x = 10; }

void main ()
{
MyClass mc;
mc.func (33); // legal: 1st func called, 'arg: 33' is printed
int x = 42;
// mc.func (x); // error: compiler can't tell which func to call
}

Leaving aside the matter of whether or not the above represents
a good design (it doesn't) or is strictly legal under the
specification
(it is, as far as I can tell), the above code does in fact compile, so
g++ evidently has a way of telling these two functions apart when
the class is defined.

However, I have not been able to find a way to explicitly invoke the
1st func or the 2nd func at will with an int variable inside main.
Any ideas?

Thank you,
 
V

Victor Bazarov

The following C++ program compiles without warning message
under RedHat 9.4 with a recent version of g++:

Either you're dreaming or g++ is run without 'strict' and 'ANSI'
options. Anyway, below I point out the three obvious errors
that render the code ill-formed.
// all put in file main.cpp for simplicity
#include <stdio.h>
class MyClass
{
public:
void func (int);
void func (int&);
}

; // missing semicolon here.
void MyClass::func (int x) { printf ("arg: %d\n", x); }
void MyClass::func (int x&) { x = 10; }

void MyClass::func(int &x) { x = 10; }
void main ()

int main ()
{
MyClass mc;
mc.func (33); // legal: 1st func called, 'arg: 33' is printed
int x = 42;
// mc.func (x); // error: compiler can't tell which func to call
}

Leaving aside the matter of whether or not the above represents
a good design (it doesn't) or is strictly legal under the
specification
(it is, as far as I can tell), the above code does in fact compile,
Nope.

so
g++ evidently has a way of telling these two functions apart when
the class is defined.

Very few g++ versions have been on my list of good compilers.
However, I have not been able to find a way to explicitly invoke the
1st func or the 2nd func at will with an int variable inside main.
Any ideas?

You can force the call to the former 'func' member by creating
a temporary:

mc.func(int(x));

There is no way to force the call to the latter 'func' (with the
single reference argument). You can trick your program by using
an intermediate variable -- a pointer to member of 'MyClass':

...
void (MyClass::*pf)(int&) = &MyClass::func;
(mc.*pf)(x);

and, of course, you can combine this into a single expression by
using a cast:

...
(mc .* static_cast<void(MyClass::*)(int&)>(&MyClass::func))(x);

It's probably just as good an idea as having two overloaded member
functions with such close argument types.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top