Clackdor Posted October 21, 2011 Share Posted October 21, 2011 How do I make use of the declaration 'const' if the variable is declared in a header file? resource.h class cPlayer { public: //stuff private: //other stuff float const movespeed; } Can't do this because I get a compiler error 'structure Player with uninitialized const members' class cPlayer { public: //stuff private: //other stuff float const movespeed=4; } Can't give it a value in a .h The code really works fine without the const, but some best practices I've been reading indicate it is a better idea for variables not affected by code to be declared as const being better for debugging than using #define or variables without const. Quote Link to comment Share on other sites More sharing options...
Canardia Posted October 21, 2011 Share Posted October 21, 2011 Class initializers are compiled into the code, so they don't use CPU cycles: #include <cstdio> class Player { // autoprotected stuff const double movespeed, turnspeed; public: Player(); const double& GetSpeed() const; }; Player::Player(): // note the doublepoint to start the class initializer list movespeed(4), // this is a class initializer turnspeed(2) // this also { } const double& Player::GetSpeed() const { return movespeed; } int main() { Player player; printf("player.movespeed = %f\n", player.GetSpeed() ); } Class initializers can be also used to call the parent classes constructor, this is very useful when you need to initialize the parent class with specific values too. Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Clackdor Posted October 21, 2011 Author Share Posted October 21, 2011 I am still getting. error: structure 'Player' with uninitialized const members in .h file class cPlayer { public: void LoadPlayer(); void UpdatePlayer(); private: const float movespeed; in .cpp file void cPlayer::LoadPlayer(): movespeed(4) { // stuff } Quote Link to comment Share on other sites More sharing options...
Canardia Posted October 21, 2011 Share Posted October 21, 2011 Class initializers must be in the constructor (Player::Player()). Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Roland Posted October 21, 2011 Share Posted October 21, 2011 class cPlayer { public: cPlayer() : movespeed(12) {} private: const float movespeed ; }; Quote Roland Strålberg Website: https://rstralberg.com Link to comment Share on other sites More sharing options...
Canardia Posted October 21, 2011 Share Posted October 21, 2011 It's bad to put code into the class declaration, Roland Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Clackdor Posted October 21, 2011 Author Share Posted October 21, 2011 Thanks Guys! Quote Link to comment Share on other sites More sharing options...
TheoLogic Posted October 24, 2011 Share Posted October 24, 2011 @Clackdor; small remark. I see you have a LoadPlayer and no constructor. You need at minimum a constructor, and even if you work with a Load/Init/... function this should be private and called from the constructor. This is rather a design remark and the idea is pretty simple: "If I create an object of your defined type, I expect it to be correctly initialized. I don't want to create + call a Load/Init/... function." By the way, in C++11 there is a new feature called "Non-static data member initialization". This means you can set the default value in the header file like you did in the first post. Quote Follow me Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.