[英]how to call another class's member function?
I have two class, class A, Class B, in class B has a static function like below: I have two class, class A, Class B, in class B has a static function like below:
class A {
public:
void method(){ B::method(); }
};
class B {
public:
static int method() {
cout << "method of b" << endl;
}
};
int main()
{
class A a;
a.method();
}
this code build error, because in class A, B is not be declared, but I want class A be defined earlier than class B, how should i do?此代码构建错误,因为在 class A 中,未声明 B,但我希望 class A 的定义早于 class B,我该怎么办? I was thought it may need forward declaration, but it seems not this reason...
我以为它可能需要前向声明,但似乎不是这个原因......
Take a look at the modified code.看一下修改后的代码。 Inline comments explain the changes:
内联注释解释了这些变化:
class A {
public:
// only declare the method
void method();
// Do NOT define it here:
// { B::method(); }
};
class B {
public:
static int method() {
std::cout << "method of b" << std::endl;
return 0;
}
};
// and now, as B implementation is visible, you can use it.
// To do this, you can define the previously declared method:
void A::method() { B::method(); }
int main()
{
class A a;
a.method();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.