how would you store string in the object

Does anybody knows how to store the received message ( 'string' )to object?

1
2
3
4
5
6
  ErrorMessage errorState;

  void NonPerishable::message(const char* string){

}


So, for this case, how would you store the received string to errorState?
What is the type of ErrorMessage? How is message being called?

Since you're working in C++, it is highly suggested that you use std::strings.
http://www.cplusplus.com/reference/string/string/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <iostream>

using ErrorMessage = std::string;

class NonPerishable {
  public:
    void message(const std::string&);
    ErrorMessage msg;
};

void NonPerishable::message(const std::string& str)
{
    msg = str;
}

int main()
{
   using std::cout;
   using std::endl;
   NonPerishable np;
   np.message("Hello, world!");
   cout << "Message = " << np.msg << endl;
}
Last edited on
Hello lithony,

What kind of object do you want to store the string in? And like Ganado asks, what is "ErrorMessage"? A class, a struct or some kind of variable?

Your little bit of code tells me nothing about "ErrorMessage" and that "errorState" is an object of "ErrorMessage".

The function definition tells me that "string", not the best choice of names here, is a C style character array which could make a difference on how you store the "string" into something else.

Oh, BTW most everything in C++ is considered an object. So when you say store the received message ( 'string' )to object I think of a class or a struct, but even a simple variable is considered an object.

Next time try to be more specific with your question and show code that better covers your question.

Hope that helps,

Andy
To put what's been said another way consider the following;

1
2
3

std::string S1;


This creates an object of type string named S1.
Topic archived. No new replies allowed.