c++ 如何创建具有变量的对象,该变量基于在构造函数中传递的另一个对象的数据

jfgube3f  于 2022-12-24  发布在  其他
关注(0)|答案(2)|浏览(117)

标题可能有点混乱,但请听我说完。我有两个类,Entity和Human。Entity是Human的父类。当我创建一个Human对象时,它需要一个Entity对象作为构造函数中的参数传递,以便我创建的所有Human对象都具有相同的Entity对象信息。
这是我的问题:如果我改变了Entity对象中的一些数据,我想更新Human对象中的所有数据,这些对象是我在构造函数中使用Entity对象创建的。
我想在我大学的一个项目中实现这个,所以我只允许使用标准库。我写了这个例子,这样它更容易理解:

#include <iostream>
using namespace std;

class Entity{
private:
    //DATA
    int life;
public:
    //DEFAULT CONSTRUCTOR
    Entity() {life = 100;}

    //PARAMETRIZED CONSTRUCTOR
    Entity(int life) {this -> life = life;}

    //GETTER
    int get_life(){return life;}

    //SETTER
    void set_life(int new_life){life = new_life;}

    //FUNCTIONS
    void print_life() {cout << "This entity has " << life << " life" << endl;}
};

class Human : public Entity{
public:
    //DATA
    string name;

    //DEFAULT CONSTRUCTOR
    Human() {name = "N/A";}

    //PARAMETRIZED CONSTRUCTOR
    Human(string name, Entity object){
        Entity::set_life(object.get_life());
        this -> name = name;
    }
};

int main(){
    //DATA
    Entity Human_data(50);
    Human Hero("Steve", Human_data);
    Human Villain("Mike", Human_data);

    //BODY
    Human_data.set_life(5000);
    Hero.print_life();
    
    //END MAIN
return 0;}

正如你所看到的,在我将人类数据寿命从50更新到5000之后,它并没有将英雄和恶棍的生命也更改为5000,而只是将人类数据寿命更改为5000。

oxf4rvwz

oxf4rvwz1#

我假设当你调用Human_data.set_life(5000)时,你想影响所有用Human_data对象构造的Human对象。
这里有一个例子,说明如何使用引用。但是注意,在类中放置引用不是没有后果的。你可以使用指针,或者更好的智能指针。但是我只是试图说明一般的想法。
从技术上讲,使用引用意味着你必须习惯使用 * 初始化列表 *,因为引用不能赋值。

#include <iostream>
using namespace std;

class Entity{
private:
    int life;
public:
    Entity() : life(100) {}
    Entity(int life) : life(life) {}
    int get_life() {return life;}
    void set_life(int new_life){life = new_life;}
    void print_life() {cout << "This entity has " << life << " life" << endl;}
};

class Human { // no inheritence
private:
    Entity& entity; // store a reference to the entity
public:
    string name;
    Human(string name, Entity& object) // object is a reference
    : entity(object), name(name)
    {
    }
    void print_life() { entity.print_life(); } // forward to entity
};

int main(){
    //DATA
    Entity Human_data(50);
    Human Hero("Steve", Human_data);
    Human Villain("Mike", Human_data);

    //BODY
    Human_data.set_life(5000);
    Hero.print_life();

    //END MAIN
    return 0;
}
5sxhfpxr

5sxhfpxr2#

如果所有的“人”也是实体(记住,继承是一种“是一种”关系),那么继承可能是正确的。
但执行上存在缺陷。
不是将Entity对象传递给Human构造函数,并使用单独且不同的Entity对象来控制“life”,而是将life本身传递给Human构造函数,并直接在Human对象上使用set_life
大概是这样的

class Human : public Entity
{
public:
    // By default humans start out with 50 in life
    Human(std::string const& name, int life = 50)
        : Entity(life), name(name)  // Initialize the parent class, then the name
    {
    }

private:
    std::string name;
};

int main()
{
    Human hero("The Hero", 5000);  // The hero will have 5000 life
    Human villain("The Villain");  // The villain will only have 50 life

    // Some stuff...

    // Update the heroes life
    hero.set_life(4000);  // Uh oh, the hero have lost 1000 life!

    // ...
}

相关问题