I have a static funcA in ClassA which calls non-static funcB in classA. Although I gave object to the funcB call I still get the error: cannot call member function without object
void* ClassA::funcA(void *arg)
{
ClassA *pC = reinterpret_cast<ClassA *>(arg);
funcB(pc);
}
void* ClassA::funcB(ClassA *arg)
{
}
what is the reason for that?
A static class method can be called without an object, like you're doing.
A regular class method needs to be called on an object, like this: objectInstance.classMethod( arguments go here ) or objectPointer->classMethod( arguments go here )
Try this (after changing the signature of funcB in your class declaration to match):
void* ClassA::funcA(void *arg)
{
ClassA *pC = reinterpret_cast<ClassA *>(arg);
pC->funcB();
}
void* ClassA::funcB()
{
...
}
The problem is that to call funcB it should be done via some object like:
pC->funcB(pC);
Actually this kind of code is more like C than C++ because if you are calling a method on an object you don't need to pass it as a parameter.
You're calling from a static method, so there's no receiver object in the scope.
Consequently, you cannot call a non-static method.
You need an object, which will receive the message: o.funcB(pc);
its not a good idea to call a member function from static function , The reason why it errors out here is that the functionB is invoked from static method . the static method cannot invoke non static member functions . The reason is static function operates on classes not on objects .
Related
in my base class, this is a public non-static member function:
void BaseClass::SetPorcessMethod()
{
//do something
Listener.support(methods::POST, this->HandlePost);
//do something
}
In the function above, Listener is a static member variable.
Function HandlePost is a pure virtual function which is implemented by all derived classes. I want to use this pointer to call different HandlePost from different derived class.
Like:
class BaseClass
{
public:
static http_listener listener;
void SetPorcessMethod();
virtual void HandlePost((http_request request) = 0;
/*listener is init in constructor , not display here*/
}
class Derived2:public BaseClass
{
void HandlePost(http_request request);
}
class Derived1:public BaseClass
{
void HandlePost(http_request request);
}
Derived1 instance1;
instance1.SetPorcessMethod();
Derived2 instance2;
instance2.SetPorcessMethod();
However, it displaysBaseClass::HandlePost:function call missing argument list use &BaseClass::HandlePost:function to create a pointer to member.I know this is because Your code attempts to pass a pointer-to-member-function, which cannot be converted to a pointer-to-function. This is because a pointer-to-member-function can only be called on an object, so it wouldn't know what object to use. Function call missing argument list to create pointer
But what should I do so that I can call the function from derived class withsupport()?
You are trying to pass a member function to support. In order to call such a function the caller need the member function arguments and the pointer to the instance to call the function on.
However support expects a std::function<void(http_request)>, i.e. without the instance pointer. So you have to wrap the call into another callable which does not need to have the BaseClass instance pointer passed. You can do that with a lambda (or std::bind if you prefer):
Listener.support( methods::POST,
[this](http_request request){return HandlePost(request);} );
Listener.support( methods::POST,
std::bind(&BaseClass::HandlePost, this, std::placeholders::_1) );
#include<functional> for the latter variant.
I'm new to C++, and I'm learning Qt.
Consider this line:
connect(ui->horizontalSlider, &QSlider::valueChanged,
ui->progressBar, &QProgressBar::setValue);
What I don't understand is why you pass the address of a static (is it static?) method valueChanged (&QSlider::valueChanged) instead of the current object method address &ui->horizontalSlider->valueChanged. Although I can use this second option that works too.
You pass the address of the member function which should be called.
The member function is not static though, that means it needs an object to work on.
class MyClass
{
void aFunction();
}
here MyClass::aFunction is a member function.
What the compiler creates is similar to this
class MyClass
{
static void aFunction(MyClass *this);
}
So whenever you call aFunction like my_instance.aFunction() the this pointer will be handed over implicitly, so the call basically becomes MyClass::aFunction(&my_instance).
As a result the address of aFunction is the same for every instance of MyClass.
Yet to execute aFunction you need an instance of MyClass.
This is why in your case you have to provide connect with both the instance ui->horizontalSlider as well the function to be called on it &QSlider::valueChanged.
What I described is an oversimplification so take it with a grain of salt.
Moreover when you have virtual functions things change.
I have some strange scenario
I have object that pass to other object its 'this' pointer
like this :
void GameLayer::startGame()
{
m_pGameController = new GameController(this);
}
in the GameController constructor i set memeber with the GameLayer
GameController::GameController(GameLayer* gamelayer)
{
m_gamelayer = gamelayer;
}
in the GameController i have functions that using callback functions
like this :
GameController::methodA()
{
CurrentGem->runAction(GameController::mycallbackMethod);
}
In the callback function I access GameController functions and members with 'this'
for example:
GameController::mycallbackMethod()
{
int test = this->age();
std::string name = this->name();
}
But the problem is when I try to access the m_pGameController it gives me exception
that says its NULL pointer
GameController::mycallbackMethod()
{
this->m_gamelayer->someGamelayerMethod();
}
The error is :
Access violation reading location 0xFFFFFFFF.
What am I doing wrong ?
When passing your callback function do call like
CurrentGem->runAction(&GameController::mycallbackMethod);
Where mycallbackMethod should be static.
CurrentGem->runAction(GameController::mycallbackMethod);
make the function mycallbackMethod static.
Side note:
To call a member function by pointer, you need two things:
a pointer to the object (or object itself)
and a pointer to the function
because pointer to member function doesn't hold reall address of the function but only offset inside object. A static member function however has no this pointer, it is the same as a regular global function, except it shares the name scope of class with other class members. So in this case you don't need pointer to object to call such callback.
Instead of having static methods and passing around a pointer to the class instance, you could use functionality of std::function and std::bind.
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().
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();
//...
}
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?
The only way to call a non static method of a class is through an instance of that class. So you would need something like this...
MyClass myClass;
myClass.myClassMethod();
I think that maybe you could use the singleton pattern, keep a instance of the class in global. like a utility class.
I am getting an error when I call one static function of a class in to any other function (means by non-class function), then it is giving following error:
undefined reference to function name_function.
Can any one tell me why this is happing?
Basic C++: A non-static member function assumes that it has access to an object of the class type (it can refer to the member variables directly and the "this" pointer points to the object).
This means that you can't call a non-static member function unless you have an object of that type. In a static member function, you don't.
(I'm not 100% sure this is an answer to your question, as it's not clean from your explanation if you tried to call a non-static function from a static one, or vise versa.)
Are you prepending the class name before the function name?
so, if you have:
class MyClass
{
...
public static function name_function() { ... }
...
}
you need to call this function like this:
MyClass::name_function();