Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

pimple

help with const inside class o_O

This topic is 5276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

this is ridiculous... using C++ builder here... anyhoo, i got this class with a constant int in it... prototypes are on header file and definitions on cpp file... this is what i have:
//## player.h ##

class CPlayer
{
   public:
          CPlayer();
          (...)
           const int MOVE,
                     GRAVITY;
}

//## player.cpp ##

#include "player.h"

CPlayer::CPlayer()
{
   (...)
   MOVE = 5;
   GRAVITY = 4;
}

if i compile that, it'll say that MOVE and GRAVITY can't be modified... so i try to initialize them on the header file right away, and it says i can't initialize class members there... what's going on? thanks! [edited by - pimple on April 13, 2004 8:43:28 PM]

Share this post


Link to post
Share on other sites
Advertisement
Try:

CPlayer::CPlayer() : MOVE(5), GRAVITY(4) {
// stuff
}


Though for this thing, static const''s or enums might be more what you have in mind.

Share this post


Link to post
Share on other sites
CPlayer::CPlayer() : MOVE(5), GRAVITY(4)
{
// blah
}
The only place where you can initialize const members is in the constructor''s initializer list.

Share this post


Link to post
Share on other sites
If they''re ''const'' and will be the same for every CPlayer, maybe you should just use a static value? (It can still be a part of the class.)

Share this post


Link to post
Share on other sites
quote:
Original post by SiCrane

Though for this thing, static const''s or enums might be more what you have in mind.


Yes, if I have constants that are part of a class I usually have them as an enum inside that class. It is a good way to make sure you don''t conflict with other enum names elsewhere.

----------
Andrew

Share this post


Link to post
Share on other sites
Another way is:


Class A
{

public:
static const int MYCONST;
}


and in the cpp


#include "A.h"

const int A::MYCONST = 10;

..other functions





[edited by - RobertC on April 13, 2004 9:39:37 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!