c++ - What does "(GameState *) new PlayingState()" do? -


the line

(gamestate *) new playingstate()  

does not make sense me. since gamestate* pointer class , new operator employed shows playingstate() object "newed" class. questions come @ end of post first definition of respective classes:

class gamestate { public:     virtual void onstart(statebasedgame &game) = 0; };  class playingstate : public gamestate, public eventsubscriber<sf::event> { public:     playingstate();     void onstart(statebasedgame &game); }; 

my questions:

1.is casting between classes? can cast classes in c++ casting data types in c? thought c++ more "formal" , "redundant"?

2.why want that? what's point of such conversion, can author achieve? i'm guessing has constructor.

3.to new something, shouldn't go format?:

<class_type> <new_class_name> = new <class_type_name> 

as in

gamestate gamestate = new playerstate; 

apparently that's not way?

without further context, odd least (and fail imagine context make sense).

first, right, in c++ style code use static_cast or dynamic_cast, depending on situation, cast between classes.

second, since playingstate inherits gamestate, pointers playingstate (as returned new playingstate()) implicitly convertible gamestate*. cast not needed @ all.

in situation, auther have used:

gamestate *gs = new playingstate(); 

or better, modern c++:

auto gs = std::make_unique<playingstate>() 

(see std::unique_ptr, std::make_unique , auto)


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -