Notbad
Notbad

Reputation: 6296

Another oddness with ObjC inheritance

I have another odd problem with Objective C inheritance. I have a protocol called IGameObject, a class GameObject that inherits from NSObject and IGameObject and finally a Player that inherits from GameObject..

The problem is that when I assign a Player* to a IGameObject*, this produces an error, but it works ok when I assign a Player* to a GameObject*. I haven't seen that this is not possible in all I have read. Here the code:

-(IGameObject*) clone
{
    Player* p=(Player*) 0xFFfFFFFF;
    //Throws an error saying that Cannont initialise a variable of type IGameObject with an value of           Player*
    IGameObject* go=p;

    //This works perfectly
    GameObject* go2=p;

    return [[Player alloc] initWithGameObject:self];
 }

Could anybody guess what is happening?

Thanks in advance.

Upvotes: 0

Views: 82

Answers (2)

jbat100
jbat100

Reputation: 16827

You cannot create a pointer to a protocol

IGameObject* go=p;

is meaningless.

Upvotes: 0

Craig Otis
Craig Otis

Reputation: 32054

When returning (or declaring) a type that is only known by its interface, don't treat it as an object pointer. Instead, use:

-(id<IGameObject>) clone {

And:

id<IGameObject> go=p;

This should clear up that warning.

Sidenote: Why in the world are you assigning p to a memory address?!

Upvotes: 4

Related Questions