Jump to content

const Declaration


Clackdor
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...