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

Pages: 1-

"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-17 21:11

Name: Anonymous 2011-11-18 12:05


class TargetWorld {
public:
  virtual void setup ();
  virtual void draw () /* const? */;
};

Name: Anonymous 2011-11-18 12:20

Please explain what is a World. I have the feeling you're overcomplicating things in a typical OOP fashion.

Name: Anonymous 2011-11-18 13:02

virtual methods. how do they work
they are bad for your health however

Name: Anonymous 2011-11-18 13:29

>>5
your
Yor "British grammar" is incorrect and deprecated.

Name: Anonymous 2011-11-18 14:01

the right path is using roles

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?

Name: Anonymous 2011-11-18 17:29

>>8
not sure about your problem but initialize your virtual function as 0.
#include <iostream>

class TargetWorld
{
    public:
        TargetWorld();
        virtual void setup() = 0;
    //
};


TargetWorld::TargetWorld()
{
    //
}

class MainMenuWorld : public TargetWorld
{
    public:
        MainMenuWorld();
        void setup();
    //
};

MainMenuWorld::MainMenuWorld()
{
    //
}

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

int main(){
  TargetWorld *tw = new MainMenuWorld();
  tw->setup();
  return 0;
}

Name: Anonymous 2011-11-18 18:11

>>9
Thank you!  That's just what I was looking for, works great.  Is this faster than having lots of switch statements to determine render, input, and drawing?  Before, it looked like:

Input thread:

switch (CurrentWorld)
{
    case Worlds::MainMenu:
        mainMenuWorld.input(event);
    break;
    case Worlds::Level:
        levelWorld.input(event);
    break;
}


Main game loop:

//gameTime computation
{
    switch (CurrentWorld)
    {
        case Worlds::MainMenu:
            mainMenuWorld.update();
        break;
        case Worlds::Level:
            levelWorld.update();
        break;
    }
}

//Drawing logic:
switch (CurrentWorld)
{
    case Worlds::MainMenu:
        mainMenuWorld.draw();
    break;
    case Worlds::Level:
        levelWorld.draw();
    break;
}


I assume using the virtual methods are a lot faster then having many switch statements.

Name: Anonymous 2011-11-18 18:43

>>10
It probably isn't faster, but it probably isn't slower unless the compiler knows the type at compile time.

Name: Anonymous 2011-11-18 19:33

>>10

Smart compilers try to unroll polymorphic methods.

Name: Anonymous 2011-11-18 19:33

>>12

otherwise polymorphic methods are just function pointers.

Name: Anonymous 2011-11-18 19:47

If it ain't Lisp, it's crap.

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