Solved

Initialize struct of strings

Posted on 2014-01-30
1
307 Views
Last Modified: 2014-01-31
I want to initialize a structure of static strings. The strings are not to be changed. I'm not quite sure if I should specify each string to be const or just the struct. And how do I get the strings in to the struct?
ClassA.h
#pragma once
#include <string>

typedef struct _myScreen
{
  std::wstring line1;
  std::wstring line2;
  std::wstring line3;
} MyScreen;

class ClassA
{
public:
  ClassA(void);
  void Test(void);
private:
  static const MyScreen ls1;
};

Open in new window

ClassA.cpp
#include "StdAfx.h"
#include "ClassA.h"

ClassA::ClassA(void)
{
  ls1 = {L"Hello", L"Goodbye", L"Friday"};
}
void ClassA::Test(void)
{
  std::wstring s = ls1.line1;
}

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
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 39823056
Since the member 'ls1' is static, there is absolutely no need to do that dynamically as in the above - just make it                

#pragma once
#include <string>

typedef struct _myScreen
{
  std::wstring line1;
  std::wstring line2;
  std::wstring line3;
} MyScreen;

class ClassA
{
public:
  ClassA(void);
  void Test(void);
private:
  static const MyScreen ls1;
};
                                  

Open in new window


and

#include "StdAfx.h"
#include "ClassA.h"

ClassA::ClassA(void)
{
}

_MyScreen ClassA::ls1 = {std::wstring(L"Hello"), std::wstring(L"Goodbye"), std::wstring(L"Friday")}; // initalize the static struct

void ClassA::Test(void)
{
  std::wstring s = ls1.line1;
}
                                  

Open in new window

0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

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…
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…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

627 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