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

Implicit casting on references

Hello,

If I have a function like :

long long getNumber();

I can use implicit casting, so I can use this function with any
compatible value, for instance:

int a = getNumber();
unsigned short b = getNumber();
long c = getNumber();
....etc...

But what happens if the method is like : ?

void getNumber(long long &number);

Now I can't do this:

int a;
getNumber(a);}

I can't even do this :

unsigned long long a;
getNumber(a);

Is there a way to do it? I know where a reference is used actually the
pointer is passed, so I see the problem in the size of the different
variables.

What bothers me is that using the second approach I would have to
define all possible values :

void getNumber(char &number);
void getNumber(short& number);
void getNumber(int& number);
void getNumber(long& number);
void getNumber(long long& number);

and also the combinations with "unsigned".

Thanks for any advice,

Luis

Feb 6 '07 #1
2 1702
kalki70 wrote:
If I have a function like :

long long getNumber();
Assuming for a moment that C++ has the type 'long long' (it does
not), your 'getNumber' function returns an r-value of that type.
I can use implicit casting, so I can use this function with any
compatible value, for instance:

int a = getNumber();
unsigned short b = getNumber();
long c = getNumber();
...etc...
Yes. Unfortunately, you're going to be losing information if
the destination type is smaller than 'long long' (assuming it
exists).
>
But what happens if the method is like : ?

void getNumber(long long &number);

Now I can't do this:

int a;
getNumber(a);}
No, you cannot. In order to pass 'a' to a function that expects
a 'long long' (assuming there is such a type), a temporary of that
type has to be created. Since the function expects a reference,
the reference will be bound to the aforementioned temporary. But
since the reference is not to a const object, it cannot be bound
to a temporary.
I can't even do this :

unsigned long long a;
getNumber(a);
No, you cannot, assuming 'long long' and 'unsigned long long' are
two different types (if they existed in C++).
Is there a way to do it? I know where a reference is used actually the
pointer is passed, so I see the problem in the size of the different
variables.

What bothers me is that using the second approach I would have to
define all possible values :

void getNumber(char &number);
void getNumber(short& number);
void getNumber(int& number);
void getNumber(long& number);
void getNumber(long long& number);

and also the combinations with "unsigned".
That's correct.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 6 '07 #2
"Victor Bazarov" <v.********@comAcast.netwrites:
kalki70 wrote:
[...]

What bothers me is that using the second approach I would have to
define all possible values :

void getNumber(char &number);
void getNumber(short& number);
void getNumber(int& number);
void getNumber(long& number);
void getNumber(long long& number);

and also the combinations with "unsigned".

That's correct.
... which is what templates were originally for. (Well, this, and
generic containers). :-)

----------------------------------------------------------------------
Dave Steffen, Ph.D. Disobey this command!
Software Engineer IV - Douglas Hofstadter
Numerica Corporation
dg@steffen a@t numerica d@ot us (remove @'s to email me)
Feb 6 '07 #3

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

Similar topics

4
by: Simon Ford | last post by:
Hi All, I'm having trouble understanding exactly how I can do some specific implicit casting. There are two problems here; does anyone know what I should be doing? //---------- // (1)...
9
by: Simon | last post by:
Hi All, Is it possible to disallow implicit casting for an operand of a function written in C? i.e. void foo(int a) {..} short b; foo(b) // error without explicit cast
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
9
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and...
6
by: Gecko | last post by:
I would like to know if there is a way to stop the runtime from implicitly casting values. For exampel, I would like the following code to crash: byte someByte = 5; int someInt = someByte; I...
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
10
by: Pieter Breed | last post by:
Hi All, Please excuse me, but the bulk of my post will be a code post. It describes some weirdness with regards to the implicit casting operator. The crux of the problem is this: I want to...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
5
by: johanatan | last post by:
Does anyone know the reasons for the lack of an implicit casting operator in any greater depth than: A. Automatic conversion is believed to be too error prone. (from the FAQ at the bottom of:...
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...
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
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.