C++ 在c++;,一个类是否可能具有包含另一个类的对象的数组?
例如,我有一个名为Apple的类和另一个名为Basket的类,我希望这个类Basket有一个私有属性,它是一个Apple对象数组 我的代码: 篮子C++ 在c++;,一个类是否可能具有包含另一个类的对象的数组?,c++,oop,object,C++,Oop,Object,例如,我有一个名为Apple的类和另一个名为Basket的类,我希望这个类Basket有一个私有属性,它是一个Apple对象数组 我的代码: 篮子 #include <iostream> #include <string> #include <apple.h> // defin basket class Basket { //class attrs; private private: std::string name;
#include <iostream>
#include <string>
#include <apple.h>
// defin basket
class Basket {
//class attrs; private
private:
std::string name;
// I want Basket objects to have an array of Apple objects
// How do I do this?
Apple [];
public: //class function
Basket(std::string); //constructor
std::string get_name() {return (name);}
};
#包括
#包括
#包括
//定义篮
班级篮{
//类属性;私有
私人:
std::字符串名;
//我希望Basket对象有一个Apple对象数组
//我该怎么做?
苹果[];
公共类函数
Basket(std::string);//构造函数
std::string get_name(){return(name);}
};
苹果
#include <iostream>
#include <string>
// defin apple
class Apple {
//class attrs; private
private:
std::string name;
public: //class function
Apple(std::string); //constructor
std::string get_name() {return (name);}
};
#包括
#包括
//定义苹果
苹果类{
//类属性;私有
私人:
std::字符串名;
公共类函数
Apple(std::string);//构造函数
std::string get_name(){return(name);}
};
您当然可以使用
Apple fruit[10];
允许篮子里最多有10个苹果。因此,您的篮子
声明如下所示:
// define basket
class Basket {
//class attrs; private
private:
std::string name;
// I want Basket objects to have an array of Apple objects
// How do I do this?
Apple fruit [10];
public: //class function
Basket(std::string); //constructor
std::string get_name() {return (name);}
};
另一种方法是创建
Apple
s的vector
。如果Apple类的构造函数没有参数,则可以将Apple数组定义为Basket类中的成员。否则您无法初始化,因为您无法初始化数组的元素。您可以这样做,这会在创建篮实例时立即创建50个苹果的数组。如果不希望这样,可以使用std::vector out.是,但必须指定大小,因为不能在编译时使用大小未定义的数组
#include <iostream>
#include <string>
#include "apple.h" // Don't use <> for your own headers.
class Basket {
private:
static const int MAX_APPLES = 10;
std::string name;
Apple apples[MAX_APPLES];
public:
Basket(std::string);
std::string get_name() {return (name);}
};
可以使用STD::vector < Apple >,但是您需要考虑STD::vector可以在场景后面调用对象的构造函数和析构函数(例如,当调用Resie()时)。这可能会导致您的对象执行一些您没有预料到的操作(例如删除一块内存并留下一个损坏的指针)
对于行为复杂的大型对象,最好使用std::vector,但是如果std::vector只是一个包含一些数据的结构,那么就可以了。尝试一个std::vector
成员只是一个建议:如果你不知道如何声明数组,如何使用它,它的语义是什么,它的各种行为方式与您预期的不同(特别是当您将其作为参数传递时),您应该在处理类之前先了解这一点。另外,正如前面提到的,最好使用std::vector而不是数组。
class Basket {
//class attrs; private
private:
std::string name;
// I want Basket objects to have an array of Apple objects
// How do I do this?
std::vector<Apple> apples;
public: //class function
Basket(std::string); //constructor
std::string get_name() {return (name);}
};
#include <iostream>
#include <string>
#include "apple.h" // Don't use <> for your own headers.
class Basket {
private:
static const int MAX_APPLES = 10;
std::string name;
Apple apples[MAX_APPLES];
public:
Basket(std::string);
std::string get_name() {return (name);}
};
#include <iostream>
#include <string>
#include <vector>
#include "apple.h"
class Basket {
private:
std::string name;
std::vector<Apple> apples;
public:
Basket(std::string);
std::string get_name() {return (name);}
};