Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

"TargetClass"

Name: Anonymous 2011-11-17 20:39

Hi there /prog/

Is something like this possible?

For example, let's say that I have two classes:

class MyWorld
{
    public:
        MyWorld();
        void setup();
        void draw();
    //
};
...
class OtherWorld
{
    public:
        OtherWorld();
        void setup();
        void draw();
    //
};


Then, I'd like to have a "TargetWorld" class, where for example:


TargetWorld targetWorld;

targetWorld = myWorld;
...
targetWorld = otherWorld;


Then, I'd like to be able to call

targetWorld.draw();
targetWorld.setup();


And that would call the initial class's method.  I was thinking about using templates or class inheritance and virtual methods, but I'm not sure if that's the right path.

Name: Anonymous 2011-11-18 16:54

>>5
Holy shit, thank you!  I was honestly surprised that there weren't any issues compiling.  It looks like this:


#ifndef TARGETWORLD_H
#define TARGETWORLD_H
class TargetWorld
{
    public:
        TargetWorld();
        virtual void setup();
    //
};
#endif


#ifndef MAINMENUWORLD_H
#define MAINMENUWORLD_H
#include "TargetWorld.h"

class MainMenuWorld : public TargetWorld
{
    public:
        MainMenuWorld();
        void setup();
    //
};
#endif
...
#include "MainMenuWorld.h"
#include <iostream>

MainMenuWorld::MainMenuWorld()
{
    //
}

void MainMenuWorld::setup()
{
    std::cout << "Reached me!" << std::endl;
}


Setting targetWorld = mainMenuWorld; and calling targetWorld.setup(); doesn't call MainMenuWorld.setup();

How could I accomplish this?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List