简体   繁体   English

C ++函数/调用何时应该是静态/非静态的?

[英]C++ When should a function/ call be static/ non-static?

I have a function defined in a C++ source file called spiral.cpp , I am trying to call that function from another C++ source file called manager.cpp . 我有一个名为spiral.cpp的C ++源文件中定义的函数,我试图从另一个名为manager.cpp C ++源文件中调用该函数。 However, I am getting a compile error that says "illegal call of non-static member function". 但是,我收到一个编译错误,上面写着“非静态调用非静态成员函数”。

I have had a look for the reasons for getting this compile error, and everything I've found so far seems to tell me that I need to make the function static in its definition. 我已经看到了获得此编译错误的原因,到目前为止我发现的所有内容似乎都告诉我,我需要在其定义中使函数为static However, if I do that, I then get a compile error that says that "'static' should not be used on member functions defined at file scope". 但是,如果我这样做,那么我会得到一个编译错误,指出“'static'不应该用在文件范围定义的成员函数上”。

The function is defined in spiral.cpp with the code: 该函数在spiral.cpp定义,代码如下:

int spiralItem::updateCircuitNumber(void){
    ...
}

It is then called in manager.cpp with the line: 然后在manager.cpp使用以下行调用它:

spiral::updateCircuitNumber();

Can anyone explain to me what I'm doing wrong here? 任何人都可以向我解释我在这里做错了什么吗? Is the problem definitely to do with the function being defined/ called as static/ non- static incorrectly, or is it something else? 问题肯定与正确定义/被称为静态/非静态的函数有关,还是其他什么?

You have to use static inside the class declaration but not in the function definition: 您必须在类声明中使用static ,但不能在函数定义中使用static

class spiralItem
{
...
public:
    static int updateCircuitNumber(void);
};

and in spiral.cpp : 并在spiral.cpp

int spiralItem::updateCircuitNumber(void){
    ...
}

You call the function in manager.cpp by writing: 您可以通过编写以下命令来调用manager.cpp的函数:

spiralItem::updateCircuitNumber();

Simply regard this as the rule for C++; 简单地认为这是C ++的规则; it does appear a little odd at first. 起初确实有些奇怪。

As mentioned in my comment, your function name makes me assume that you actually want to manipulate state of a particular spiral instance. 正如我的评论中所提到的,你的函数名称让我假设你实际上想要操纵特定spiral实例的状态。 In this case, you would need (or at least usually use) a member function. 在这种情况下,您需要(或至少通常使用) 成员函数。 It seems this is what you actually did. 这似乎是你实际做的。 Check. 校验。 Now, in order to call this function, you need to have an instance of spiral that you can call the function on. 现在,为了调用此函数,您需要有一个可以调用函数的spiral实例。

spiral s;
s.updateCircuitNumber();

or (if allocated dynamically) 或(如果动态分配)

spiral * s = new spiral();  // better use appropriate smart_ptr but that's another story
s->updateCircuitNumber();

These calls change the state of that particular s object. 这些调用改变特定的状态s对象。 If you have another spiral object s2 , this remains unchanged and you are free to call updateCircuitNumber() for that instance as well as needed. 如果你有另一个spiral对象s2 ,它保持不变,你可以随意调用该实例的updateCircuitNumber() Non-static member functions are also called instance functions because they operate on a particluar instance of a class. 非静态成员函数也称为实例函数,因为它们在类的特定实例上运行。

static functions are also referred to as class functions because they are independent of a particular instance of that class. static函数也称为类函数,因为它们独立于该类的特定实例。 Therefore, they may only access static members of that class, but not non-static ones (as these only exist for a particular instance which is not available). 因此,它们只能访问该类的静态成员,而不能访问非静态成员(因为这些只存在于不可用的特定实例中)。 As mentioned in the other answers, the static keyword is only added inside the class declaration. 如其他答案中所述, static关键字仅添加在类声明中。 Static function can then be called as you tried with spiral::updateCircuitNumber(); 然后可以在尝试使用spiral::updateCircuitNumber();调用静态函数spiral::updateCircuitNumber(); .

You can have static member functions in a class. 您可以在类中使用静态成员函数。 First thing to note here is that static member functions don't have this pointer that means you cannot access any data member/non-static member functions of class in static function as they requires this pointer.Second point is that static member function can be called without instantiating a class. 首先要注意的是静态成员函数没有这个指针,这意味着你不能访问静态函数中类的任何数据成员/非静态成员函数,因为它们需要这个指针。第二点是静态成员函数可以是在没有实例化类的情况下调用。

For eg:- 例如: -

class One
{
private:
   static int count;
public:
  static int getCount ()
  {
    return count;
  }
};

You have to initialize count in .cpp file:- 你必须在.cpp文件中初始化计数: -

int One::count = 0;

And then you can call member function like this:- 然后你可以像这样调用成员函数: -

std::cout << One::getCount () << std::endl;

Your error is probably in the header file. 您的错误可能在头文件中。 How do you declare your static method? 你如何申报静态方法?

Here is how it should look like considering your example: 以下是考虑您的示例的方式:

class spiralItem {
public:
  static int updateCircuitNumber(void);
};

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

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