Solved

CString class constants

Posted on 2002-06-19
4
282 Views
Last Modified: 2013-11-20
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
Comment
Question by:hohn
4 Comments
 
LVL 4

Expert Comment

by:mblat
ID: 7093718
class MyClass
{
public:
 MyClass(const CString cs) : m_cs(cs) {}

protected:
 const CString m_cs;
};

....
MyClass cl("AAAA");
0
 

Author Comment

by:hohn
ID: 7093748
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
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 7094265
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
 
LVL 2

Accepted Solution

by:
tdubroff earned 100 total points
ID: 7099228
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 55
Strange date stored 22 165
Excel Use VBA to get user's Mac Address for their computer 5 322
java ^ examples 8 69
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question