Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Implicit casting on references

0 views
Skip to first unread message

kalki70

unread,
Feb 6, 2007, 11:22:15 AM2/6/07
to
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

Victor Bazarov

unread,
Feb 6, 2007, 11:30:26 AM2/6/07
to
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


Dave Steffen

unread,
Feb 6, 2007, 2:31:27 PM2/6/07
to
"Victor Bazarov" <v.Aba...@comAcast.net> writes:

> 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)

0 new messages