c++ - 如何在头文件的结构中获取变量? C++

标签 c++ variables struct

基本上我的一个头文件已经被更改,其中返回某些变量的函数已被删除,我现在不知道如何获取变量。任何人都可以对此有所了解。

函数getX()getY() 已从头文件中删除,不允许我以任何方式添加/修改头文件。有没有办法我仍然可以从我的 main.cpp 中获取 x 和 y 的值?:

struct Point
{
    int x;
    int y;

    Point ()                {   x = NULL;   y = NULL;   }
    Point (int x1, int y1)  {   x = x1;     y = y1;     }
    ~Point (void)           {   }


    Point & operator= (const Point &p)
    {   x = p.x;    y = p.y;    return (*this);     }

    bool operator== (const Point &p)
    {   return ( (x == p.x) && (y == p.y) );        } 

    bool operator!= (const Point &p)
    {   return ( (x != p.x) || (y != p.y) );        } 

    // 2 points are 'connected' but 'different' if they : 
    // i)  share the same 'x' but adjacent 'y' values, OR
    // ii) share the same 'y' but adjacent 'x' values!!
    bool isConnected (Point &p)
    {
        return (    ((x == p.x) && ( ((y-1) == p.y) || ((y+1) == p.y) )) || 
                    ((y == p.y) && ( ((x-1) == p.x) || ((x+1) == p.x) ))
               );
    }

    void display (std::ostream &outputStream=std::cout)     
    {   outputStream << "[" << x << ", " << y << "]";   }


    ============================================================
    // This two functions are now removed. =====================
    ============================================================
    int getX() // Removed.
    {
        return x;
    }

    int getY() // Removed.
    {
        return y;
    }

};

我之前使用这两个函数的部分:

int deadendX = pointOne.getX();
int deadendY = pointOne.getY();

那么现在把头文件中的函数都去掉了,有没有什么办法呢?比如我可以在 main.cpp 中编写一些函数来执行此操作吗?

最佳答案

这应该可以解决问题:

int deadendX = pointOne.x;
int deadendY = pointOne.y;

x 和 y 是 Point 的公共(public)成员变量,因此您可以访问它们。

关于c++ - 如何在头文件的结构中获取变量? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42370352/

相关文章:

c++ - 引用基类的成员 var 导致访问冲突

c++ - 错误 : no matching function for call to

c - 使用 printf 的大数字

php - 如何将javascript变量设置为php session 变量

c++ - 在 C++ 中重载一元运算符有困难吗?

c++ - 许多情况下的开关优化保证任何情况下均等的访问时间? ( C++ )

django - 如何将变量从 settings.py 传递到 View ?

c# - 如何在 C# 中将结构转换为字节数组?

c - 如何理解链表结构中的指针 'next'?

C 结构 : what does this mean?