Solved

C++ initialize const strings in class?

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Title # Comments Views Activity
maze travler 6 62
How can i compile this github project?? 2 90
How to remove Recent Projects from Embarcadero C++ builder XE10. Berlin 2 66
Need some help with mailto 16 20
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

860 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