Implicit casting on references

K

kalki70

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
 
V

Victor Bazarov

kalki70 said:
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
 
D

Dave Steffen

Victor Bazarov said:
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). :)
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top