Solved

CString class constants

Posted on 2002-06-19
4
280 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Process filename extension 3 186
How to know only "File created" with EventLog 3 34
java ^ examples 8 67
scoreUp challenge 14 63
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: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

810 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