Reputation: 9930
Is this code legal?
It crashes and I'm not sure if it's because of my programming environment (it's an embedded device) or because I've written dodgy code.
int foo(NGKGame * game) {
game->init(); //Crashes here
return 0;
}
int main() {
NGKGame * game = new PlaneGame();
game->init(); //This call is okay
foo(game);
return 0;
}
NGKGame
is the base class. PlaneGame
is a derived class from NGKGame
. init()
is a pure virtual function in the base class but is implemented in PlaneGame
.
Thanks,
Edit:
The init function being called twice is just as part of the example. In my current implementation, it's empty.
Upvotes: 0
Views: 104
Reputation: 1420
The code looks fine. It might have to do with some compiler flags. I had a somewhat similar issue a while ago. A code which juggles around pointers, would work fine with no optimization flags (using gcc), but would crash with the -O3(optimization) turned on. I did not figure out how to get rid of that, so I just manually added various optimization flags that were enabled with -O3 and it worked fine.
Upvotes: 0
Reputation: 63797
There's nothing wrong with your snippet.
Though are you meant to call NGKGame::init
twice or is it just part of your example code? My hunch is that something inside PlaneGame::init
is malfunctioning when calling it again, if that is the case.
Upvotes: 0
Reputation: 87396
It's probably crashing because you are calling init()
twice on the same object. Since you didn't show us the source code of init()
I can't speculate on why it would crash, but it seems like a function named init
should only be called once.
If you want a better answer, please see http://sscce.org/
Upvotes: 2
Reputation: 6834
This code is fine - assuming its OK for you to call init()
twice.
That init()
is PV in NGKGame
is not a problem. The whole point of PVs is to allow you to call them in the manner of foo()
.
Upvotes: 1