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