Solved

CString class constants

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Generic progress indicator 6 110
Line meaning 9 77
noX challenge 17 77
Problem to open Excel file 15 44
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now