简体   繁体   English

如何从上面的 class 调用 function? class 未被继承

[英]how do I call the function from the upper class? the class is not inherited

class a{

private:
        class b* b = new b;
        int getSomeWhatValue(){
              
        }
};

class b{
private:
        void bFunc(){
               //I want to call an getsomewhatvalue() here
        }
};

I made the code really simply.我把代码写得很简单。 How do I call an a's function at b?如何在 b 调用 a 的 function? I tried to use this pointer, but didn't work:(我试图使用this指针,但没有用:(

Your scenario will cause a cyclic calling (ie never ending loop of A creating an instance of B which in tern creates an instance of A and the loop continues), so its not permissible.您的场景将导致循环调用(即 A 的永无止境的循环创建 B 的实例,该实例在 tern 创建 A 的实例并且循环继续),因此它是不允许的。

You have two options:你有两个选择:

  1. Declare "int getSomeWhatValue()" as static, and call it through the class (instead through an instance).将“int getSomeWhatValue()”声明为 static,并通过 class(而不是通过实例)调用它。
  2. Pass a pointer to the function you want to call (int getSomeWhatValue()) and delcare the prototype somewhere outside of class A or B.将指针传递给您要调用的 function (int getSomeWhatValue()) 并在 class A 或 B 之外的某处删除原型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM