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

reference vs. int arg confusion

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,

Nov 3 '07 #1
1 1325
sp**************@yahoo.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 3 '07 #2

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

Similar topics

9
by: Sandy | last post by:
Hi, In one of my interview I was asked a question, whether using pointers for argument is efficient then reference or not. i.e. void fun(Complex *p) void fun(Complex &ref) can somebody...
4
by: JMCN | last post by:
object invalid or no longer set - confusion of the recordset in access 2003. i am currently converting from access 97 to access 2003. majority of the codes converted over perfectly fine, though...
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
1
by: Richard Lewis Haggard | last post by:
I'm having a problem with what appears to be some sort of confusion with references. I have a single solution with a dozen projects which has been working quite nicely for a while. The references...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
2
by: AMDRIT | last post by:
Hello Everyone, I am having an issue with my solution and hoping that you all can suggest a resolution. I have a common library that (A) that three other projects make reference of (B,C,D). ...
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
0
by: Justin | last post by:
When creating an executible received this error during the linking... g++32 -L/usr/lib/x86_64-redhat-linux3E/lib64 -o confusion cdb.o conindb.o confusdb.o confusion.o ../../arc/lib/arc.a...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.