Solved

Initialize struct of strings

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

735 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