首页 > c++ > 涉及继承和私有对象的特定问题,c ++

涉及继承和私有对象的特定问题,c ++ (specific problem involving inheritance and a private object, c++)

问题

我将尝试尽可能简洁地解决问题:

我有3个类,一个机器人类,Point类和一个世界级。

class Point
{
private:
    int x;
    int y;

public:
    void set(int x, int y)
    {
        this->x = x;
        this->y = y;
    }

    int getX()
    {
        return x;
    }

    int getY()
    {
        return y;
    }
};

class Robot{
private:
    Point roboLocale;
public:
    Robot()
    {

    }

    void init()
    {
        roboLocale.set(0, 0);
    }

class World
{
private:
    char arr[10][10];
public:
    World()
    {
        std::fill(arr[0], arr[0] + 10 * 10, ' ');

    }
    void injectRobot()
    {
        arr[roboLocale.getX()][roboLocale.getY()] = '@'; // would like to access robotLocales x and y coords
    }

    void displayField()
    {
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                std::cout << "[" << arr[i][j] << "]";
            }
            std::cout << std::endl;
        }
    }
};


int main()
{
    World field;
    Robot robot;
    robot.init();

    field.injectRobot();

    field.displayField();
}

在我的World类下,void injectRobot()我试图访问robotLocalegetX()和getY()成员函数来“注入”机器人到我的World::arr。我根本无法弄清楚如何做到这一点,或者甚至可能。任何帮助将不胜感激。

解决方法

injectRobot不知道任何Robot情况。应该怎么知道的程序,其他地方(你main)有哪些应该被用于获取一个实例roboLocale对象,除了是私人Robot课吗?

  1. injectRobot必须有一个函数参数,你传入Robot要注入的实例(或至少它的位置,如果这是你唯一感兴趣的东西)。
  2. 您的injectRobotmuste能够实际获取存储在该Robot对象中的位置信息。

你可以尝试:

#include <array>
#include <iostream>

class Point
{
private:
    int _x{};
    int _y{};

public:

    // Default constructor to prevent uninitialized values
    Point() = default;

    Point(int x, int y) 
        : _x(x), _y(y) { }

    void set(int x, int y)
    {
        _x = x;
        _y = y;
    }

    int x() const { return _x; }
    int y() const { return _y; }
};

class Robot 
{
private:
    Point _robolocale;
public:
    Robot() = default;
    Robot(int x, int y) : _robolocale(x, y) {}
    Point const& pos() const { return _robolocale; }
};

class World
{
private:
    char arr[10][10];
public:
    World()
    {
        std::fill(arr[0], arr[0] + 10 * 10, ' ');
    }
    // Which robot should be injected? 
    // We need to pass a robot in because otherwise there is none to inject
    void injectRobot(Robot const& r)
    {
        arr[r.pos().x()][r.pos().y()] = '@'; // would like to access robotLocales x and y coords
    }

    void displayField()
    {
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                std::cout << "[" << arr[i][j] << "]";
            }
            std::cout << std::endl;
        }
    }
};


int main()
{
    World field;
    Robot robot1;
    Robot robot2(7, 4);

    field.injectRobot(robot1);
    field.injectRobot(robot2);

    field.displayField();
}

问题

I'm going to try to make my problem as concise as possible:

I have 3 classes, a robot class, Point class and a world class.

class Point
{
private:
    int x;
    int y;

public:
    void set(int x, int y)
    {
        this->x = x;
        this->y = y;
    }

    int getX()
    {
        return x;
    }

    int getY()
    {
        return y;
    }
};

class Robot{
private:
    Point roboLocale;
public:
    Robot()
    {

    }

    void init()
    {
        roboLocale.set(0, 0);
    }

class World
{
private:
    char arr[10][10];
public:
    World()
    {
        std::fill(arr[0], arr[0] + 10 * 10, ' ');

    }
    void injectRobot()
    {
        arr[roboLocale.getX()][roboLocale.getY()] = '@'; // would like to access robotLocales x and y coords
    }

    void displayField()
    {
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                std::cout << "[" << arr[i][j] << "]";
            }
            std::cout << std::endl;
        }
    }
};


int main()
{
    World field;
    Robot robot;
    robot.init();

    field.injectRobot();

    field.displayField();
}

In my World class under void injectRobot() I am trying to access the robotLocale getX() and getY() member functions to 'inject' the robot into my World::arr. I simply can not figure out how to do this, or if it's even possible. Any help would be greatly appreciated.

解决方法

Your injectRobot does not know about any Robot instances. How should it know that somewhere else in the program (in your main) there is an instance which should be used to fetch the roboLocale object which in addition is private to the Robot class?

  1. Your injectRobot must has a function parameter where you pass in the Robot instance to be injected (or at least its position if that is the only thing you're interested in).
  2. Your injectRobot muste be able to actually fetch the positional information stored inside that Robot object.

You could try:

#include <array>
#include <iostream>

class Point
{
private:
    int _x{};
    int _y{};

public:

    // Default constructor to prevent uninitialized values
    Point() = default;

    Point(int x, int y) 
        : _x(x), _y(y) { }

    void set(int x, int y)
    {
        _x = x;
        _y = y;
    }

    int x() const { return _x; }
    int y() const { return _y; }
};

class Robot 
{
private:
    Point _robolocale;
public:
    Robot() = default;
    Robot(int x, int y) : _robolocale(x, y) {}
    Point const& pos() const { return _robolocale; }
};

class World
{
private:
    char arr[10][10];
public:
    World()
    {
        std::fill(arr[0], arr[0] + 10 * 10, ' ');
    }
    // Which robot should be injected? 
    // We need to pass a robot in because otherwise there is none to inject
    void injectRobot(Robot const& r)
    {
        arr[r.pos().x()][r.pos().y()] = '@'; // would like to access robotLocales x and y coords
    }

    void displayField()
    {
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                std::cout << "[" << arr[i][j] << "]";
            }
            std::cout << std::endl;
        }
    }
};


int main()
{
    World field;
    Robot robot1;
    Robot robot2(7, 4);

    field.injectRobot(robot1);
    field.injectRobot(robot2);

    field.displayField();
}
相似信息