Noble-Surfer
Noble-Surfer

Reputation: 3172

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. 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. 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".

The function is defined in spiral.cpp with the code:

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

It is then called in manager.cpp with the line:

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?

Upvotes: 3

Views: 8196

Answers (4)

Bathsheba
Bathsheba

Reputation: 234665

You have to use static inside the class declaration but not in the function definition:

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

and in spiral.cpp:

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

You call the function in manager.cpp by writing:

spiralItem::updateCircuitNumber();

Simply regard this as the rule for C++; it does appear a little odd at first.

Upvotes: 3

Johannes S.
Johannes S.

Reputation: 4626

As mentioned in my comment, your function name makes me assume that you actually want to manipulate state of a particular spiral instance. 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 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 sobject. If you have another spiralobject s2, this remains unchanged and you are free to call updateCircuitNumber()for that instance as well as needed. 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. 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 function can then be called as you tried with spiral::updateCircuitNumber();.

Upvotes: 4

qstebom
qstebom

Reputation: 739

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

Upvotes: 1

ravi
ravi

Reputation: 10733

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 e.g:-

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

You have to initialize count in .cpp file:-

int One::count = 0;

And then you can call member function like this:-

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

Upvotes: 1

Related Questions