从另一个类访问变量/函数
Accessing variables/functions from another class
我正在制作一个纸牌游戏,我有几个类。我有一个手牌类,一个玩家类,一个"列"类(纸牌在手牌之后被放置在屏幕上),我需要每个类都能访问其他类的变量。
class Hand
{
private:
int **Hx,Hy;** //Hand X, Hand Y
int HAmount;//Amount of cards in Hand
int HOwner; //Player 1/2
int Limit; //Limit of cards in Hand
int HContents[8]; //Card Position in 54 card deck NOT card value.
bool Removed;
public:
Hand();
void Lim();
void Get_Card();
void Show();
void Set_Values(int y, int Own);
};
然后在另一个类中,我需要访问上面的一些变量。
void Card::show()
{
if((apply == true)
{
if((Track == true)&&(SelNum == TNum)&&(TOwner == COwner))
{
ScnPos = TAmount;
x = Tx;
y = Ty + ScnPos*10;
}
if((Hand == true)&&(**HOwner** == COwner))
{
x = **Hx** + ScnPos*45;
y = **Hy;**
}
apply_surface(x,y,Cards,Screen,&Clip[Pos]);
}
}
我试过用班级友谊和其他方法,但我不能使它工作。(显然我有更多的变量需要同样的处理)(忽略代码中的任何错误)
代码中的错误才是真正的问题。Card
没有理由访问Hand
的私有成员。这是设计错误,你的其他问题只是想告诉你。
那么,您应该将getters
和setters
作为变量。例如:
class Test {
private: int a;
public: int GetA() {
return this->a;
}
void SetA(int a) {
this->a = a;
}
}
相关文章:
- 未使用的C++未优化的静态成员函数/变量
- 内联函数/变量的外部链接
- 同一函数中的静态函数变量初始化顺序
- 将非静态成员函数C 的使用无效,而将成员功能作为函数变量
- 如何从doxygen输出中排除类的无证件成员函数/变量
- 如何在LUA C API中分配全局LUA函数变量
- 如何将函数变量传递给C lambda函数
- 通过另一个C++函数更改函数变量
- c++11 lambda 真的支持闭包吗?函数变量中存在语义冲突
- C++:静态成员不能在声明时定义,但静态函数变量可以?
- apache-frift是否支持函数变量作为参数
- 类函数/变量在使用之前必须声明
- 函数变量去哪里
- 无法使用函数变量?错误表达式必须具有类类型
- 如何在main中使用函数变量
- 每当尝试打印函数变量时,C++ cout 错误
- 是否存在查看成员函数/变量的整洁等效项
- 访问友元类之间的函数变量
- 函数到函数变量
- 一个定义规则是否强制创建一个静态函数变量