Archived

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

declaring const config values within classes other than ints

This topic is 5129 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

I''m interested in saving some basic configuration info in my class. The problem is that the values I want to use are objects that need to be constructed. For example, say I wanted to store the default gravity as a Vector(0, 0, -9.8) and the default sound as a Sound("boing.wav"). What I *want* is to be able to put at the top of my Environment class something like:
class Environment
{
const Sound defaultSound = Sound("boing.wav");
const Vector gravity = Vector(0, 0, -9.8);

...
Obviously, that''s not legal. The closest thing I can come up with is:
class Environment
{
Sound defaultSound;
Vector gravity;

Environment()
{
defaultSound = Sound("boing.wav");
gravity = Vector(0, 0, -9.8);
}

...
I find the second way very cumbersome and unintuitive - and worst of all, I can''t make those values const. Does anyone see what I''m talking about, and know a way to get close to the desired effect? Thanks for any feedback~

Share this post


Link to post
Share on other sites

class Class {
Class(Type value1, Type value2) : constMember1(value1), constMember2(value2) {}
const Type constMember1;
const Type constMember2;
static const Type staticConstMember;
};

const Type Class::staticConstMember = value;



[edited by - eighty on November 28, 2003 1:58:05 AM]

Share this post


Link to post
Share on other sites