Ok. so i'm in a bit of an unusual situation trying to structure this program and I Just don't know how to do it logically
Ok basically its getting to be able to draw whats defined in a different class.
this is my general situation. (minimum code to understand the situation, not real testable code)
see i could do this. and it work fine. but i need to take "thisstuff" and put it in a different class for organisation.
so i would try this
1 2 3 4 5 6
class stuff{
public:
// dont have access to window to make a draw.
private:
stuffneeded thisstuff;
};
1 2 3 4 5 6 7
class Game : public stuff{
private:
window gamewindow;
public:
void dowhatineed(){
//gamewindow.draw(thisstuff); <-- cant
}
well now i don't have access to gamewindow to draw "thisstuff".
wouldnt it be bad to inherit privately? isn't that the same as basically global variables?
// A little ugly on one file
// I'm just making everything public so I type less
// Also I'm using manual memory management
// simply to keep it short and sweet
#include <iostream>
struct Window;
struct Renderable
{
virtualvoid render( Window& ) = 0;
};
struct Window
{
void render( Renderable& renderable )
{
// give the objects this window for operations
renderable.render( *this );
}
void print( constchar* str )
{
std::cout << str << '\n';
}
};
struct Square : public Renderable
{
void render( Window& window )
{
// use the window
window.print( "A square" );
}
};
struct Circle : public Renderable
{
void render( Window& window )
{
window.print( "A circle" );
}
};
struct Game
{
Window window;
Renderable *renderables[2];
void update( void )
{
for ( int i = 0; i < 2; ++i )
{
// tell the window to render the objects
window.render( *renderables[i] );
}
}
};
int main( void )
{
Game the_game;
the_game.renderables[0] = new Square();
the_game.renderables[1] = new Circle();
the_game.update();
for ( int i = 0; i < 2 ; ++i )
delete the_game.renderables[i];
return 0;
}