Solved

C++ initialize const strings in class?

Posted on 2014-01-25
4
662 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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

16 Experts available now in Live!

Get 1:1 Help Now