c++ Setting a pointer variable in parent class from child and use it in parent class -


i'm sorry title. seem have problem. i'm beginner , i'm sorry if asked before.. couldnt find straight answer on one. (when search class, pointer , child results passing parent or child pointers... not want pass (this) child or parent pointer, want pass pointer initialized on child class.. parent). i'm trying here better explained code:

class app { public:     virtual void init(void)         { window = &basicwindow(); }     virtual void createwindow(void) { window->create(); }  protected:     window *window; };    class game : public app { public:     virtual void init(void)         { window = &openglwindow(); } };  int main () {     app *game = &game();     game->init();     game->createwindow();     return 0; } 

is legal? have abstract window class basicwindow , openglwindow derives. however, when create window access violation reading location error breaking @ window->create() inside app::createwindow() function.

thanks

i'm guessing because pointing temporary:

window = &basicwindow() 

once function exits, window points "crap" , bad things happen.

presumably, want create new instance of window - i.e.

window = new basicwindow(); 

don't forget cleanup!


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -