Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a Non Static Member Method from Another Method

Is there a way to call a non static class member method from another method that is contained within the main class in c++? If so, what would the code look like?

Problem is, I can't declare this specfic method as static, because it uses other methods within the same class that then don't work if I make the one static.

I'm trying to use:

MyClass::myClassMethod();

from a method within the main class, but it gives me the error: a non static member reference must be relative to a specific object.

To clarify, myClassMethod() uses other methods within MyClass like:

void myClassMethod() {
    ...
    anotherClassMethod();
}

so if I were to make myClassMethod static it would interfere with calling anotherClassMethod().

like image 918
aclark Avatar asked Mar 15 '12 03:03

aclark


People also ask

How do you call a non static method from another method in Java?

But when we try to call Non static function i.e, TestMethod() inside static function it gives an error - “An object refernce is required for non-static field, member or Property 'Program. TestMethod()”. So we need to create an instance of the class to call the non-static method.

Can I call non static method from static method?

A static method can call only other static methods; it cannot call a non-static method.

Can we call non static method from main method?

Since you want to call a non-static method from main, you just need to create an object of that class consisting non-static method and then you will be able to call the method using objectname.


2 Answers

What is the deal with calling non-static member function from a static member function?

Every non static member function is passed an this pointer implicitly in addition to the parameters you pass, the pointer passed is then dereferenced to refer class object members However static functions are not passed with the implicit thispointer and hence one cannot call any non static member function inside a static member function because there is no this to do so.

What is the solution, If you want to do it anyways?

You will need some mechanism to get the pointer to the object inside the static method and then you can call the member function using that pointer.
How to do that?
You will have to store the pointer to class object globally, or pass it as an instance in one of the function arguments to the static method.

However, both of above are workarounds, the important thing to note here is If you feel the need of calling a non static member function through a static member function then there is something wrong in your design.


On Second thoughts maybe I mis-read your Question before, Probably, Your question is:

How to call a non-static member function of a class from main?

You need a instance of the class to call non-static member functions.
So simply,

MyClass obj; 
obj.myClassMethod();   

And calling any other member function from within myClassMethod() would simply be:

void myClassMethod()
{
   //...
   anyOtherMyClassNonStaticMemberFunction(); 
   //...
}
like image 123
Alok Save Avatar answered Nov 05 '22 05:11

Alok Save


A static method is one that doesn't run on any particular object. It's a lot like a standalone function outside of a class, except that it's allowed to access private members in its class.

If you anotherClassMethod() is non-static, that means it has to be called on a specific object, an instance of the class. Because it's called on an object, it can access data stored in that object (non-static member variables). If myClassMethod() is static and you implement it as

void MyClass::myClassMethod() {
  anotherClassMethod();
}

That won't work because anotherClassMethod() expects to be called on a specific object, but myClassMethod() doesn't have one. But if you know what object you want to call it on, you can do it as an ordinary method call on an object:

void MyClass::myClassMethod(MyClass &object) {
  object.anotherClassMethod();
}

The object doesn't have to be passed in as an argument; it could be a static member variable in the class, for example:

class MyClass {
private:
  static MyClass theInstance;

  // ...
};

void MyClass::myClassMethod() {
  theInstance.anotherClassMethod();
}

Ultimately, the question you need to ask yourself is: why is myClassMethod() static, and why is anotherClassMethod() non-static? Take a step back, think about what myClassMethod() is supposed to do. Does it make sense to call it when you don't have an instance to work with? If so, why does it need to call a method that expects to work with an instance?

like image 33
Wyzard Avatar answered Nov 05 '22 04:11

Wyzard