PDA

View Full Version : SDK: C++ Design question...


RustyKnight
01-15-2008, 11:13 PM
Okay, I seem to be running into this bit lately, and since it's been a LOOONG while since I coded into C++, I can't quite remeber if there is away around it...

Basically, I want to objects/classes to reference each other...

For example...

class Tool {

public Tool(Shed* shed);

}

class Shed {

Tool* getContents();

}

This will cause a compile error, because the compiler does not yet know about shed...

I thought about providing a "global" pointer to "shed" that could be accessed by tools, but I'd still run into the same problem.

Any ideas would be helpful...

Cheers
Shane

RustyKnight
01-15-2008, 11:40 PM
Okay, so I've learnt about forward declarations, this is okay, but are there any other methods??

Shane

scorpion007
01-16-2008, 12:35 AM
You could redesign it so there's a one-way dependency, instead of a circular one.

Otherwise, sure, forward declare it.

RustyKnight
01-16-2008, 01:18 AM
You could redesign it so there's a one-way dependency, instead of a circular one.

Otherwise, sure, forward declare it.Thanks for that. It would nice not to have the dependency, but I ran across a situation that can't be helped...

Cheers
Shane

susanta
01-16-2008, 03:19 PM
What about this way?

class Shed;
class Tool
{

public: Tool(Shed* shed);

};

class Shed
{

Tool* getContents();

};

RustyKnight
01-16-2008, 09:17 PM
What about this way?

class Shed;
class Tool
{

public: Tool(Shed* shed);

};

class Shed
{

Tool* getContents();

};Yeah, that's pretty much what I ended up with...so use to late binding

Shane

CGTalk Moderation
01-16-2008, 09:17 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.