Solved

Initialize struct of strings

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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

11 Experts available now in Live!

Get 1:1 Help Now