[英]Use member function of another class without object and avoid static
This question is different from those duplicated ones.这个问题不同于那些重复的问题。
I have 2 classes A and B. B has a member function B::boy
that uses its variable b
.我有 2 个类 A 和 B。 B 有一个成员函数
B::boy
使用其变量b
。
A has a member function A::evaluate
and in this member function, I need to use B::boy
. A 有一个成员函数
A::evaluate
,在这个成员函数中,我需要使用B::boy
。
The problem now is that :现在的问题是:
1) B::boy
cannot be static because it needs to use a non-static variable b
. 1)
B::boy
不能是静态的,因为它需要使用非静态变量b
。
2) I cannot instantiate a object of B in A. Because I have one object of B elsewhere and that stores the variable b
that is also useful in A. 2) 我不能在 A 中实例化 B 的对象。因为我在别处有一个 B 的对象,它存储了在 A 中也有用的变量
b
。
Is it not possible to be done in C++?用 C++ 是不可能完成的吗?
将b
作为A::evaluate
的参数传递,或者在 A 中有 B 的指针或引用成员(取决于b
值在A
类中的“有用”程度)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.