简体   繁体   English

使用另一个没有对象的类的成员函数并避免静态

[英]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.

相关问题 非静态成员函数的无效使用 - 类成员函数调用另一个类成员函数 - Invalid use of non-static member function - Class Member Function Calling Another Class Member Function 另一个类成员函数中另一个类的对象 - Object of another class in another class member function 从另一个类访问静态成员函数 - Accessing a static member function from another class 将非静态成员 function 传递给另一个 class 的成员 function - Pass non-static member function to member function of another class 如何在另一个类的静态成员函数中访问私有静态变量? - How to access private static variable in static member function of another class? 初始化是另一个类对象的静态类成员 - Initializing static class member which is another class object How can I call a non-static member function of a class from a static member function of another class? - How can I call a non-static member function of a class from a static member function of another class? 如何在另一个类的静态成员函数中传递非静态成员函数的函数指针 - How can I pass function pointer of a non static member function in a static member function in another class 可以在没有 object 的情况下调用 class 成员 function 吗? - Can a class member function be invoked without an object? 在静态类函数成员中获取类对象指针 - Getting class object pointer in static class function member
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM