C++中的静态函数调用非静态函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13331447/
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 17:12:04  来源:igfitidea点击:

Static function call non static function in C++

c++static

提问by CJAN.LEE

I have a class like::

我有一个像::

Class Test
{
  public:
  void Check(){//dosomething};
  static void call(){//I want to call check()};
};

Because call() is a static member, so it can't call non-static functions, so I think to use Check() in call() is to create Test pointer and then point to Check(), but I think it is not good, is there a better way to do this? I can rewrite all of things in the static function, so I don't need to call Check() again, but what I want is to reuse the code in Check() and avoid repeated code.

因为call()是静态成员,所以不能调用非静态函数,所以我认为在call()中使用Check()就是创建Test指针然后指向Check(),但我认为是不好,有没有更好的方法来做到这一点?我可以在静态函数中重写所有东西,所以我不需要再次调用Check(),但我想要的是重用Check()中的代码,避免重复代码。

回答by Axel

Since you need an instance, you either have to create one, use a static instance, or pas it to call():

由于您需要一个实例,您必须创建一个,使用静态实例,或将其传递给call()

Class Test
{
  private:
  static Test instance;

  public:
  void Check(){//dosomething};
  // use one of the following:
  static void call(Test& t){ t.check(); };
  static void call(){ Test t; t.check(); };
  static void call(){ instance.check(); };
};

回答by W. Goeman

This sounds like there is some bad design going on.

这听起来像是进行了一些糟糕的设计。

Anyhow, what you can do, is create an instance of Test in call, and call Check on that instance. The implementation of call would be something like this then:

无论如何,您可以做的是在调用中创建一个 Test 实例,并在该实例上调用 Check 。调用的实现将是这样的:

void call(){
  Test test;
  test.Check();
}

However, note that if Check does something with the members of Test, it will ofcourse only apply to the created Test object. I would rethink whether you really want call to be static, or Check not to be.

但是,请注意,如果 Check 对 Test 的成员进行了某些操作,则它当然仅适用于创建的 Test 对象。我会重新考虑您是否真的希望 call 是静态的,或者 Check 不是。

回答by john

There's no simple answer to this. There are various things you could do, but which is right depends on what your code means. It's a designquestion, not a programming question.

对此没有简单的答案。您可以做各种事情,但哪一种是正确的取决于您的代码的含义。这是一个设计问题,而不是一个编程问题。

You've already suggested various programming tricks you could do, like creating a Testpointer (actually you don't need a Testpointer, just a Testobject). I could suggest more tricks, for instance you could rewrite call()so that it's not static, or (very nearly the same thing) you could pass a Testpointer as a parameter of call()and use that, or you could create a global Testobject and use that. None of these get to the heart of the problem. To answer your question you have to ask yourself questions like, why did I make call()static in the first place, why does a static function need to call a non-static function.

您已经建议了您可以执行的各种编程技巧,例如创建Test指针(实际上您不需要Test指针,只需要一个Test对象)。我可以建议更多的技巧,例如您可以重写call()使其不是静态的,或者(几乎相同的事情)您可以将Test指针作为参数传递call()并使用它,或者您可以创建一个全局Test对象并使用它。这些都没有触及问题的核心。要回答您的问题,您必须问自己这样的问题,为什么我首先要使call()静态,为什么静态函数需要调用非静态函数。

If you can explain that, then it's easier to give more specific advice.

如果你能解释这一点,那么提供更具体的建议会更容易。

回答by Dietmar Kühl

The key different between a non-static and a static member function is that the latter doesn't have any object. It still has the same access privileges like all other members, however.

非静态成员函数和静态成员函数之间的主要区别在于后者没有任何对象。但是,它仍然具有与所有其他成员相同的访问权限。

When you want to call a non-static member function from a static member, you still need to come up with an object, however. Often, the static member function would get passed in some context to get to an object. From the sounds of your question it seems that the static and the non-static functions are meant to do similar things which don't require and object. In this case it is probably best to factor the common part, not depending on any object, into another function which is then called from both call()and Check():

但是,当您想从静态成员调用非静态成员函数时,您仍然需要提出一个对象。通常,静态成员函数会在某些上下文中传递以获取对象。从你的问题的声音来看,静态和非静态函数似乎是为了做不需要和对象的类似事情。在这种情况下,最好将不依赖于任何对象的公共部分分解为另一个函数,然后从call()和调用该函数Check()

void Test::call() {
    common();
    // ... other code
}
void Test::Check() {
    common();
    // ... other code, possibly depending on "this"
}
void Test::common() {
    // logic shared by both call() and Check() but not depending on "this"
}

If the common code needs an object, there is no other way than coming up with an object in your static member function.

如果公共代码需要一个对象,那么除了在静态成员函数中提出一个对象之外别无他法。