Solved

Initialize struct of strings

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now