We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › int & long duplicate methods
Page Index Toggle Pages: 1
int & long duplicate methods (Read 516 times)
int & long duplicate methods
Oct 19th, 2007, 4:08pm
 
I have 2 methods to compute 2^k:

 int pow2(int k)  { return ((int)1<<k); }
 long pow2(int k) { return ((long)1<<k); }

This gives me options, for example:

 int a = pow2(30);  // 2^30=1073741824
 long b = pow2(31); // 2^31=2147483648

Note that
 int a = pow2(31); // 2^31 = -2147483648
would give an overflow.

However I get an error:
 Duplicate declaration of method "pow2"

Should not the compiler distinguish those 2 methods?

I could just always use the long version, casting to int if necessary, but it is a bit annoying.

Any comment?


Re: int & long duplicate methods
Reply #1 - Oct 19th, 2007, 4:36pm
 
alphachapmtl wrote on Oct 19th, 2007, 4:08pm:
Should not the compiler distinguish those 2 methods

No. The method signature doesn't include the return type...
So basically, you can't do that.
Re: int & long duplicate methods
Reply #2 - Oct 26th, 2007, 8:05pm
 
jorgecardoso wrote on Oct 19th, 2007, 4:36pm:
No. The method signature doesn't include the return type...
So basically, you can't do that.


But that suggests you could change the parameter type:

long pow2(long k) { return ((long) 1<<k); }

And now call it like this:
long b = pow2((long) 31);

-- djones
Page Index Toggle Pages: 1