Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

CString class constants

Who can suggest a "clean" method for initializing a class constant of type CString? This is what I currently do:

class MyClass {
public:
       .....

protected:

static CString tag;    //= "<mytag>";

...
}

and in the .cpp file...

CString MyClass ::tag = "<mytag>";

This is kind of ugly. I really would rather have constants.

Any ideas?
0
hohn
Asked:
hohn
1 Solution
 
mblatCommented:
class MyClass
{
public:
 MyClass(const CString cs) : m_cs(cs) {}

protected:
 const CString m_cs;
};

....
MyClass cl("AAAA");
0
 
hohnAuthor Commented:
A reasonable answer,  mblat. Thanks. Unfortunately, it's not really appropriate to my "real code" case, I must say. The literal values really do belong encapsulated and readable (for documentation purposes) inside the class. Besides, I am using dozens of CString class constants. Perhaps I should have mentioned those details. I wonder if it's just a shortcoming of the language?

-Hohn
0
 
Jan LouwerensCommented:
just use a static const variable:


class MyClass {
public:
      .....

protected:

static const CString tag;    //= "<mytag>";

...
}

and in the .cpp file...

const CString MyClass::tag = "<mytag>";

// maybe you need the alternative form of initializer
const CString Myclass::tag("<mytag>");

// (Sorry, I can't test this right now, so I apologize for any mistakes)

0
 
tdubroffCommented:
hohn,

Other than adding a const modifier as per jlouwere's suggestion, you are doing it the only way you can for 'static' variables.  Static variables of course are class variables and not instance variables; every object of that class sees the same values for the static variables.  

Each static variable consumes its own location in memory.  That is why you've placed the definition of the static var in the cpp file and not in the header file.  If you had placed the definition in the header file, then in every cpp file where the header file was included another independent copy of the same-named variable would get instantiated.   Usually not a good thing.

If what you really want is constants that can be seen in your header file, it can't be done with statics.  But, what you can do is something like the following (no guarantees if this actually compiles, but you should get the gist):


// Header file
class MyClass {
public:
   enum {
      MY_FIRST_TAG,
      MY_SECOND_TAG,
      MY_THIRD_TAG
   };
protected:
   static CString tag[3];
   . . .
};

// Cpp file
CString MyClass::tag[] = { "<mytag1>", "<mytag2>", "<mytag3>" };

. . .

cout << tag[MY_FIRST_TAG] << endl;


     

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now