Solved

CString class constants

Posted on 2002-06-19
4
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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