Ok, I looked it up and I *think* I've implemented a very basic version.
However, I have one problem, when I try to access a member function of the sub-class that's been created through the factory, it says that the base class doesn't have that function.
Here's what I have:
SpriteFactory.h
Code:
class SpriteFactory
{
public:
virtual Sprite* getSprite(int)=0;
};
class ConcreteFactory : public SpriteFactory
{
public:
virtual Sprite* getSprite(int x)
{
if(strcmp(sprname[x].c_str(),"EndPoint")==0)
return new EndPoint(xsprcrds[x],ysprcrds[x]);
if(strcmp(sprname[x].c_str(),"LeftCloud")==0)
return new LeftCloud(xsprcrds[x],ysprcrds[x]);
if(strcmp(sprname[x].c_str(),"LeftEndGreenBlock")==0)
return new LeftEndGreenBlock(xsprcrds[x],ysprcrds[x]);
if(strcmp(sprname[x].c_str(),"MidCloud")==0)
return new MidCloud(xsprcrds[x],ysprcrds[x]);
if(strcmp(sprname[x].c_str(),"MidGreenBlock")==0)
return new MidGreenBlock(xsprcrds[x],ysprcrds[x]);
if(strcmp(sprname[x].c_str(),"RightCloud")==0)
return new RightCloud(xsprcrds[x],ysprcrds[x]);
if(strcmp(sprname[x].c_str(),"RightEndGreenBlock")==0)
return new RightEndGreenBlock(xsprcrds[x],ysprcrds[x]);
if(strcmp(sprname[x].c_str(),"StoneBlock")==0)
return new StoneBlock(xsprcrds[x],ysprcrds[x]);
};
};
And here's how I use it in my main file:
Code:
SpriteFactory* factory = new ConcreteFactory();
Sprite* test = factory->getSprite(0);
...
test->draw(); //Get errors here
Now, at the test->draw(); part that I get errors, I get the error:
error: 'class Sprite' has no member named 'draw'
but the draw member function IS in the subclass! It's not in the base Sprite class, but it's in the sub class!! So does anyone have any idea why this is happening?
Thanks for the replies getting me this far, any further help's also appreciated.