declaring const config values within classes other than ints

Started by
1 comment, last by rileyriley 20 years, 4 months ago
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~
--Riley
Advertisement
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]
Thanks for the reply - I guess I''ll go with initializer lists, and use #defines to control them in the header file.
--Riley

This topic is closed to new replies.

Advertisement