Solved

C++ initialize const strings in class?

Posted on 2014-01-25
4
719 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
[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
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

724 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