Solved

C++ initialize const strings in class?

Posted on 2014-01-25
4
674 Views
Last Modified: 2014-01-25
Say I have a class with a lot of constant strings in it:
#pragma once
class ClassA
{
public:
  ClassA(void);
  ~ClassA(void);
private:
  const std::wstring textA;
  const std::wstring textB;
  const std::wstring textC;
  ...
  const std::wstring textZ;
};

Open in new window

How can I instantiate this class and fill in all the strings, especially if this class is initialized in the constructor initializer list of another class?
#pragma once
#include "ClassA.h"

class ClassB
{
public:
  ClassB(void);
  ~ClassB(void);
private:
  ClassA a;
};

Open in new window

0
Comment
Question by:deleyd
  • 3
4 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 39809387
Add the constructor to initialize the constants:
   ClassA(std::wstring tA) : textA(tA), textB(tA), textC(tA), textZ(tA) {}
   ClassB(std::wstring bA) : a(bA) {}

This makes them all the same. Customize to your needs.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 39809410
If those constants are the same for all instances of ClassA, then you should make them static. If you do that, you can define their values in the corresponding .cpp file. In this case, you would not include them in the constructor.
0
 

Author Comment

by:deleyd
ID: 39809473
Each class will have different constants.

I'm going to end up with:

ClassA(std::wstring tA,
std::wstring tB,
std::wstring tC,
...
std::wstring tD) : textA(tA), textB(tB), textC(tC),... textZ(tZ) {}

And the initializer is going to look even worse. Plus I'm discovering problems in the initializer are difficult to debug. The debugger stops but doesn't tell me where in this huge list the problem is.
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 39809526
Create a struct to hold the constant values (but don't make them const in the struct). Add a constructor to take in the struct, and the constructor will load the struct into a const struct.
struct Constants {
  std::wstring textA;
  std::wstring textB;
  std::wstring textC;
  std::wstring textZ;
};

class ClassA
{
public:
   ClassA(void) {}
   ClassA(Constants myConst) : myConstants(myConst) {}
   ~ClassA(void) {}
private:
   const Constants myConstants;
};

class ClassB
{
public:
   ClassB(void) {}
   ClassB(Constants bA) : a(bA) {}
   ~ClassB(void) {}
private:
  ClassA a;
};


int main() {
   Constants tempNonConstValues;
   // ... fill in non-const local constants
   ClassB cb(tempNonConstValues);
}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

930 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