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

c++ abstraction halp plz

Name: Anonymous 2008-03-26 1:57

    alright, i need c++ help again, you guys delivered for me the other night, heres hoping you'll help again.

    the context is a space sim. I have a class called ship which holds data members for a 3d position, a throttle, speed, a name and an engine.

    now i have a method called move ship which calculates the ships speed based on the throttle integer, the max speed and the acceleration factor and adds that value to the ships current position. The problem is the members accel and max_spd are stored in the engine class, this isn't a problem except i want to abstract the ship and the engine class so i can call child classes of both shp and engine so i can in essence reuse that code to make as many ship types as I want out of the same two peices of code...

    so my first try was to simply make a ship class with a data member function that calls the engine class. i.e.

    class ship{
    Engine m_engine
    }

    so far so good until i try to derive that out like so

    sSpartan : public ship
    {
    eng_mk1 m_engine;
    ]
    eng_mk1 is a class derivation of Engine, it always initilized the Engine m_engine part in the base class constructor, and eng_mk1 object is never created

    I'm pretty sure im going to have to rely on virtual functions... I get the idea, but no the syntax. halp plz?

Name: Anonymous 2008-03-26 3:24

*snicker*
Throw more polymorphism/encapsulation in there, that will fix your problems.

Name: Anonymous 2008-03-26 7:19

TRY WRITING IT IN JAVA

Name: Anonymous 2008-03-26 13:26

This is why OOP is a terrible thing to teach newbie programmers.

Your ``Spartan'' is a fucking ship. It performs like a ship and does everything a ship does, so make it a ship. A ship has characteristics like engines, weapons and shit, so add those in as appropriate -

typedef struct {
    int     type;
    void   *engine;
    void   *weapons;
    void   *shit;
} Ship;

enum {
    TYPE_TROLL,
    TYPE_SPAM,
    TYPE_SPARTAN
};

typedef struct {
    int   type;
    void *othershit;
} Engine;

enum {
    ENGINE_MK1,
    ENGINE_FLYING_FUCK
};

Ship alloc_spartan() {
    Ship s;
    s.type = TYPE_SPARTAN;
    s.engine = malloc( sizeof( Engine ) );
    ((Engine*) s.engine).type = ENGINE_MK1;
    /* etc, do your faggot shit here */
    return s;
}


tl;dr read sicp.

Name: Anonymous 2008-03-26 13:27

you can use pointers:

class ship{
Engine *m_engine;
ship(){
m_engine = new Engine();
}
~ship(){
delete m_engine;
}
};

ckass sSpartan : public ship{
sSpartan(){
m_engine = new eng_mk1();
};

Engine::~Engine() should be virtual

Name: Anonymous 2008-03-26 13:40

you can use null pointers:

class ship{
Engine *m_engine;
ship(){
m_engine = NULL;
}
~ship(){
delete m_engine;
}
};

Name: Anonymous 2008-03-26 13:43

>>6
What good would that do?

Name: Anonymous 2008-03-26 13:59

THIS IS SPARTA

Name: Anonymous 2008-03-26 14:18

>>6
you can use whitespace:

whitespace
    is
        meaningful

Name: Anonymous 2008-03-26 14:21

the context is a space sim. I have a class called ship which holds data members for a 3d position, a throttle, speed, a name and an engine.

Actually, I didn't see that. Then your fucking ship structure should just be --

typedef struct {
    double x, y, z,
       throttle, speed;
    char* name;
    int engine;
} Ship;


Where engine is either an enumeration representing the engine type, or if you want to dick around with different bullshit (how different can two engines possibly be?) a pointer to an instance of another ``Engine'' struct which has the appropriate characters, and a vtable if absolutely necessary.

I fucking swear you beginner sepplers are fucking retarded. They should go back to teaching K&R.

Name: Anonymous 2008-03-26 14:28

>>5
Also, as a final comment for this round: if you're going to fucking use some RAII, use it for everything. Arguably RAII is one of the only ``good'' features that Sepples has going for it, since it allows you to maintain your heap with the stack, but all of you fucking idiots just go on and off with it.

If you're going to insist on using RAII, use it for everything:


class ship{
    std::auto_ptr< Engine > m_engine;

    ship(){
        m_engine = std::auto_ptr< Engine >( new Engine() );
    }

    ~ship() {}

private:
    ship( const ship& ) {}
    const ship& operator=( const ship& ) {}
};


Guess what? This suddenly makes the incredibly stupid errors in your copy semantics much more apparent, so your whole application doesn't crash and burn when you try to put your shit in a standard container (or other bullshit).

Enjoy your Sepplaids, while I laugh as it knifes you in the back.

Name: Anonymous 2008-03-26 17:14

Goddamn beginner sepplers trolling us exponentially >.<

Name: Anonymous 2010-11-25 13:55

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