discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why is this casting needed?


From: Helge Hess
Subject: Re: Why is this casting needed?
Date: Fri, 24 Mar 2006 23:48:05 +0100

On Mar 24, 2006, at 21:17, Nate Smith wrote:
Hello,

I have two classes, A, and B, each which have "initWithNumber:" methods.
I have another class, C, which accepts an instantiated A object in one
of its constructors. I compile the following 'code':

C *c = [[C alloc] initWithA: [[A alloc] initWithNumber: 5]];

The compiler gives me a warning saying that "initWithNumber:" has two
definitions: one in A, and one in B. If I cast the result of [ A alloc ]
to (A*), no error is reported:

C *c = [[C alloc] initWithA: [(A*)[ A alloc ] initWithNumber: 5]];

Is this a bug or a feature? If GNUstep is using my regular GCC, my GCC
version is 3.4.5.

This is a necessity, the compiler needs to know the type signature of the method to generate proper code. At least if it involves base types.

The return value of +alloc is 'id', so the compiler doesn't know the static class of the receiver of the 'initWithNumber:' method and therefore has two options. If you tell him using the cast, he can properly find the method declaration.

This is why you should avoid using the same selector with different signatures.

Greets,
  Helge
--
http://docs.opengroupware.org/Members/helge/
OpenGroupware.org





reply via email to

[Prev in Thread] Current Thread [Next in Thread]