Solved

Initialize struct of strings

Posted on 2014-01-30
1
303 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 how to clear a vector as well as how to detect empty vectors in C++.

856 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