Solved

C++ initialize const strings in class?

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

749 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