简体   繁体   中英

Can I put a list in a struct?

I am trying to use a list as part of a struct due to the fact our products come in several colors. Is this some thing I can do or should i just use an array? Either way i have not found any examples on the web.

#include "stdafx.h"
#include<iostream>  
#include<string>  
#include<sstream>  
#include<cctype> 
#include<list>

using namespace std;

////////////////////////////////////////////////////////////////////////////////////////
//  Constances
////////////////////////////////////////////////////////////////////////////////////////

#define N_PRODUCT 3

struct Brand_t {
int Model_Num;
string Product_Name;
list<string> Colors;
} brands [N_COLOR];

////////////////////////////////////////////////////////////////////////////////////////
//  Functions
////////////////////////////////////////////////////////////////////////////////////////


int main()
{
string mystr;
int n;

for (n=0; n < N_PRODUCT; n++)
{
    cout << "Enter Model Number: ";
    std::getline (cin,mystr);
    stringstream(mystr) >> brands[n].model_Num,4;
    cout << "Enter Product Name: ";
    getline(cin,classrooms[n].Product_Name);
    list<string>::iterator it;
    Students.push_back ("Red");
    Students.push_back ("Yellow");
    Students.push_back ("Blue");
}

return 0;
}

Yes, this can be done. Thanks to the RAII, the list object will be automatically initialized and freed based on the lifetime of your struct. Take note that even thought this is not the case in other programming languages such as Object Pascal, the struct and class are effectively the same thing in C++, the only difference being default visibility of member methods and variables, as other answers have noted.

You can not put non- PODS objects into unions though.

I suggest you use a std::vector or a std::array instead of a C-style array. A std::vector would probably be most useful if your array is supposed to be of dynamic size. If you use plain new or even malloc() , then your objects will NOT be automatically freed (not even initialized with malloc() )

Yes, there is no reason not to. The only differences between a struct and a class in C++ are

A) struct members are public by default, and

B) struct inheritance is public by default (ie, struct A : B is equivalent to class A : public B where B is a class .)

Using a list, or any object, inside of a struct is perfectly acceptable as long as the object has or supports a default constructor.

The default constructor is required because when a struct variable is first declared, it is also initialized. Any object inside of it will also be initialized by calling the default constructor.

There's nothing wrong with having an instance of a class as a member of a struct in C++, UNLESS you're relying on C++ treating instance of the struct as POD (plain old data) objects. (Like with bit-wise copying, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM