Jump to content

  • Log In with Google      Sign In   
  • Create Account


Some game architect problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 lucky6969b   Members   -  Reputation: 586

Like
0Likes
Like

Posted 27 February 2013 - 10:46 PM

class Humanoid
{
void Walk();
void Jump();
void Talk();
}

class Human : public Humanoid
{
void MuckAround();
}

class Goblin : public Humanoid
{

}

class Main
{
vector <Humanoid*> objs;
}


objs.push_back(new Human());
objs.push_back(new Goblin());
std::vector::iterator it, itEnd = objs.end();
for (it = objs.begin(); it != itEnd; it++)
{
*it->Talk();
*it->MuckAround();
}

 

I think the human object still can't muck around. Is there a major flaw in this design?

Thanks Jack


Edited by lucky6969b, 27 February 2013 - 10:50 PM.


Sponsor:

#2 johnmarinelli   Members   -  Reputation: 304

Like
0Likes
Like

Posted 28 February 2013 - 12:20 AM

I'm not sure I understand your question, but I can see that when the you try to call MuckAround() on a Goblin object it would crash at runtime because the Humanoid class doesn't define that function - is this what you were wondering?



#3 Tribad   Members   -  Reputation: 841

Like
0Likes
Like

Posted 28 February 2013 - 12:49 AM

I'm not sure I understand your question, but I can see that when the you try to call MuckAround() on a Goblin object it would crash at runtime because the Humanoid class doesn't define that function - is this what you were wondering?

This code will not compile.

Because MuckAround is not defined in Humanoid. So you cant call it on the base class.



#4 Vincent_M   Members   -  Reputation: 619

Like
0Likes
Like

Posted 28 February 2013 - 01:30 AM

What you're looking for are virtual members (base class functions). That MuckAround() method in Humanoid should be:

virtual void MuckAround() {}

In your class definition. Sorry if you don't see that in code tags but I'm on my phone atm. If the return type isn't "void", change it to what it should be and return some default value, and it'll compile when called.

EDIT: my bad, I skimmed through it. Looks like you're just trying to call the subclass method, not override. As a rule of thumb, if you declare a method, always define it.

#5 lucky6969b   Members   -  Reputation: 586

Like
0Likes
Like

Posted 28 February 2013 - 03:31 AM

I think Vincent's suggestion is what I am looking for.

Thanks folks

Jack



#6 skullfire   Members   -  Reputation: 349

Like
0Likes
Like

Posted 28 February 2013 - 08:19 AM

Hi lucky,

 

I know what you asked has probably already been answered, but looking at your design based on inheritance, I feel there's a need to at least tell you about architectural decisions in a game engine. As your game grows and you need to keep on adding functions and different classes over the inheritance system, things start to mess up. You need to copy some code from one class to another that does not directly correlate via inheritance.

 

Some bibliography:

http://cmpmedia.vo.llnwd.net/o1/vault/gdccanada09/slides/marcinchadyGDCCanada.ppt

http://www.learn-cocos2d.com/2010/06/prefer-composition-inheritance/

http://gamearchitect.net/2008/06/01/an-anatomy-of-despair-aggregation-over-inheritance/

 

Hope this sheds some light if not for this, for future projects.

Andy






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS