Can a Structure Use a string Class Member?

Can you use a string class object instead of a character array for the name member? That is, can you declare a structure like this:

#include <string>struct inflatable   // structure definition{    std::string name;    float volume;    double price;};

The answer is yes unless you are using an obsolete compiler that does not support initialization of structures with string class members.

Make sure that the structure definition has access to the std namespace. You can do this by moving the using directive so that it is above the structure definition. The better choice, as shown previously, is to declare name as having type std::string.

Get C++ Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.