English 中文(简体)
如何在另一类的对象上调用一个类的函数?
原标题:How to Call Function of one class on the object of another class?

我怎样才能在一个班级上 使用另一个班级上的一种方法呢?

我有;

class A { 
 public :
  foo ( ) ;
};

class B {
 public :
  bar ( ) ;
};

主数 :

A data ;          // I am creating instance of class A 

data . bar ( ) ;  //  but, I am calling a method of another class

                  // how can I do that ?

注意: 我找不到合适的标题 。 如果您有, 请随意分享或编辑

问题回答

除非这两类人有血缘关系(通过继承),否则你不能这样做。

A member functions performs some action on the instance of the class to which it belongs.
You created an object of class A so you can only call member functions of A through it.

磨苹果,希望得到芒果奶昔, 不会真的发生正确的。

使用公共遗产:

class B {
 public:
  void bar();
};

class A : public B
{ };

int main() {
 A a;
 a.bar();
}

我认为,如果您想要在对象A上使用.bar (), B 就必须继承 。

您想要 data.bar () 做什么并不清楚 。

bar() as no access to A s data, so bar() cannot have anything to do with the variable data. So, I would argue, that data.bar() is unnecessary, you are aiming for just bar(). Presumably, if bar() is just a function, you can declare it static and call B.data()

另一个选择是,您想要其他人已经写过的遗产。 当心遗产继承, 并且确保您继承 B 的 A 只有在您有某种关系的情况下才能继承 。 它满足了 < a href=" http:// en.wikipedia. org/wiki/Liskov_ substitution_ principle" rel=“ nofollow” > Liskov principles 。 不要因为您必须拨打 < code>bar < () 而继承 B 。

如果您想要使用 B, 您可以在 A 中使用 B 实例。 读到 < a href=" http:// en.wikipedia.org/wiki/Composition_ over_ insheritance" rel=“ nofollow” > 相对于继承的构成而言, 偏重组成

As everyone said in their answers. Its a bad idea and not possible.

你只能用没人真正知道 它会如何表现的把戏

你可以得到一个物体A的指针 并把它投入B的旁观者。

这样做的唯一用途是展示其他不做的事情。

A a;
B* b = (B*)&a;
b->bar();

我认为你应该读1或2c+2c加书,

一些建议:由Bjarne Stroustrupp或C++考虑的c++编程由Bruce Eckel编写,或者在网上搜索教程。

您可以使用函数指针。使其不静态的唯一办法是使用模板。

class A
{ 
  public:
  void setBar(void (*B::func)(void)) { bar = func; };
  void callBar() { bar(); };

  private:
  void(*B::bar)(void);
};

class B
{
  public:
  static void bar() { printf("you called bar!"); };
};

int main() {
  A a;
  a.setBar(B::bar);
  a.callBar();
}

您也可以将等级 B 声明为等级 friend A friend

我认为它的语法是:

class A {
    public:
        foo();
    friend class B;
};
class B {
    public:
        bar();
};

But with this, I believe you can only use functions/variables from A inside B functions. Inheritance will probably be your better approach to it.

Although this question is strange !, but here are some solutions Using inheritance

class A: public B

铸铸类型

A data;
((B*)&data)->bar();

或重新解释

B* b = reinterpret_cast <B*> (&data);
b->bar();

如果 bar () 使用 B 中的任何成员变量,则结果不可预测。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?