C++ 如何在另一个类的对象上调用一个类的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10772245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:26:29  来源:igfitidea点击:

How to Call Function of one class on the object of another class?

c++oopclassfunctionfunction-call

提问by user1414276

How can I call one method in one class over using another class ?

如何在一个类中调用一个方法而不是使用另一个类?

I have ;

我有 ;

class A { 
 public :
  foo ( ) ;
};

class B {
 public :
  bar ( ) ;
};

in main :

在主要:

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 ?

Note : I could not find a appropriate title. If you have one, feel free to share or edit

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

回答by Alok Save

Unless the two classes are related(through inheritance) You cannot do that.

除非这两个类是相关的(通过继承),否则您不能这样做。

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

成员函数对它所属的类的实例执行一些操作。
您创建了一个类的对象,A因此您只能A通过它调用成员函数。

Grinding an Apple and hoping to get a mango shake, won't really happen right.

磨一个苹果并希望得到一个芒果奶昔,这不会真的发生。

回答by starius

Use public inheritance:

使用公共继承:

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

class A : public B
{ };

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

回答by Chip

It is not clear what you want data.bar()to do.

目前还不清楚你想做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 staticand call B.data()

bar()由于无法访问 A 的数据,因此bar()与变量 没有任何关系data。所以,我认为,这data.bar()是不必要的,你的目标只是bar(). 据推测,如果bar()只是一个函数,你可以声明它static并调用B.data()

The other option is that you wanted inheritance which some other people have already written about. Be careful with inheritance, and make sure you inherit A from B only if you there is a is-a relationship, and it satisfies the Liskov Principle. Don't inherit from B just because you have to call bar().

另一种选择是您想要继承其他人已经写过的内容。小心继承,并确保只有在存在 is-a 关系时才从 B 继承 A,并且它满足Liskov 原则。不要仅仅因为您必须调用bar().

If you want to use B, you can have a instance of B inside A. Read about prefering composition over inheritance

如果你想使用 B,你可以在 A 里面有一个 B 的实例。阅读关于优先组合而不是继承

回答by Horonchik

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

正如每个人在他们的回答中所说的那样。这是一个坏主意,不可能。

You can only use tricks that no one really knows how its gonna behave.

你只能使用没有人真正知道它会如何表现的技巧。

You can get the pointer of an object A and cast it to be poiter of B.

您可以获取对象 A 的指针并将其强制转换为 B 的指针。

Again the only use of that is to show other what not to do.

同样,唯一的用途是向其他人展示不要做的事情。

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

回答by vikramjitSingh

I think you should read 1 or 2 c plus plus book(s) and get a fair idea what classes are about and what purpose they are meant to serve.

我认为您应该阅读 1 或 2 本书加上书籍,并了解课程的内容以及课程的目的。

Some suggestions: The c++ programing by Bjarne Stroustrup or Thinking in c++ by Bruce Eckel or search over net for tutorials.

一些建议:Bjarne Stroustrup 的 c++ 编程或 Bruce Eckel 的 Thinking in c++ 或在网上搜索教程。

回答by aj.toulan

You can use a function pointer. The only way to make it not static is to use templates.

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

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();
}

回答by JustThatTypeOfGuy

You can also declare class Bas a friendof class A.

您还可以将 class 声明B为 a friendof class A

I believe the syntax for it is:

我相信它的语法是:

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

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

但是有了这个,我相信你只能使用函数A内部的B函数/变量。继承可能是你更好的方法。

回答by Mateusz Rogulski

I think if you want use .bar() on A object A must inherit by B.

我认为如果你想在 A 对象上使用 .bar() 必须由 B 继承。

回答by Aimar Hassano

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

虽然这个问题很奇怪!,但这里有一些解决方案 使用继承

class A: public B

Type cast

类型转换

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

Or reinterpret cast

或者重新解释演员表

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

If bar() use any member variables of B, then the result is not predictable.

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