在对象的另一个类中创建实例 [英] Creating instance in an another class of an object

查看:65
本文介绍了在对象的另一个类中创建实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Plant的Crop类中创建一个实例,我无法设法找到一种方法来使该工作完成.希望你们能为您提供帮助.

I need to create an instance in class Crop of Plant class and i cant manage to find a way to make that work. Hope you guys can help at that.

这是我向对象作物发送对象的主要地方.

This my main where i'm sending an object to the class crop.

int main(){

char select = 'a';
int plant_num = 2;
int crop_num = 0;

Crop crop[10];
Plant plant[20];

plant[0] = Plant("Carrots",'c');    
plant[1] = Plant("Lettuce",'l');
plant[2] = Plant("Rosemary",'r');

crop[0] = Crop(plant[0],4,2);
crop_num++;

cout << "Garden Designer" << endl;
cout << "===============";

}

我想要植物类实例的地方

class crop is where i want the instance of plant class

class Crop {

private:
    int g_length;
    int w_width;
    Plant PlantType;

public:
    Crop();
    Crop(const Plant&, int, int);

};  

我要在作物类中作为实例的

类植物

class plant of which i want the instance of in class crop

class Plant {

private:
    char plant[20];
    char p_symbol;

public: 
    Plant();
    Plant(const char* n, char s);
};

Crop.cpp

#include <iostream>
#include <iomanip>
#include <cstring>
#include <new>
#include "crop.h"
using namespace std;


Crop::Crop(){



}

 Crop::Crop(const Plant& temp, int l, int w){



}

对不起,如果我错过了一些东西.真的很困惑,如果您需要Plant.cpp文件的内容,请问我.我认为不需要该文件.

sorry if im missing something. really confused if you need Plant.cpp file content just ask me. I didn't think that file would be needed.

推荐答案

有些东西叫做成员初始化器列表,并且它的位置在其参数列表之后的构造函数定义中,其前面是分号,然后是构造函数的主体.因此,要初始化成员,您可以编写

There is something called a Member initializer list and its place is in the constructors definition after its parameters list, preceded by a semicolon, followed by a body of a constructor. So to initialize members you can write

Crop::Crop(const Plant& temp, int l, int w) : 
  g_length(l), 
  w_width(w), 
  PlantType(temp) 
{ }

这是调用成员的适当构造函数.他们不必是副本构造函数.您可以显式地调用default或其他任何默认值,尽管在您的情况下这可能没有多大意义.

What this does is call the appropriate constructors of the members. They don't have to be copy constructors. You can explicitly call default one or any else, although it probably does not make much sense in your case.

这是因为在执行构造函数的主体之前实例化了成员.您可以在 int s中使用它们,因为它们可以设置在主体内部.但是,引用必须始终有效,因此体内没有任何内容将具有空引用".

It is because the members are instantiated before executing the body of the constructor. You would not have problem with ints, as they can be set inside the body. References, however, need to be valid at all the times, so nothing inside the body will have a "null-reference".

您可以这样做:

Crop::Crop(const Plant& temp, int l, int w) : PlantType(temp) 
{ 
   g_length = l;
   w_width = w;
}

但是任何未初始化的成员都将使用默认构造函数进行初始化,因此在//此处 g_lenght 存在且具有值(0或垃圾桶,我不知道)回想一下,如果默认值是零,我认为这是th回),然后在正文中调用 operator = .在第一种情况下,对象是通过复制构造函数创建的.

But any member not initialized explicitly gets initialized with a default constructor, so at //here g_lenght exists and has a value (either 0 or trash i don't recall if default is zero, i think it's thrash), and then in the body operator= gets called. While the 1st case, object is created via copy constructor.

通常这并没有太大的区别,但是对于更复杂的类型,这可能很重要.特别是如果构造一个空对象要花费很长时间,那么赋值也很复杂.只是比设置对象并使用 operator = 重置对象更好地设置对象一次.

Often this is not a big difference, however for more complicated types it might be important. Especially if constructing an empty object takes long time, and then assignment is also complicated. It's simply to better set-up object once, than create it and reset with operator=.

我个人出于某种原因喜欢2nd的外观,但这被认为是较差的做法,因为从逻辑上讲,这些成员从一开始就应该使用某些值来构造.

I personally like the look of 2nd for some reason, but it is considered a worse practice, because logically those members are meant to be constructed using some value from the start.

这篇关于在对象的另一个类中创建实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆