This question already has answers here:
C++ How to pass member function pointer to another class?
(2 answers)
How can I pass a member function where a free function is expected?
(9 answers)
Calling C++ member functions via a function pointer
(10 answers)
Closed 2 years ago.
Next answers not responding my question:
C++ How to pass member function pointer to another class? (2 answers)
How can I pass a member function where a free function is expected? (9 answers)
Calling C++ class methods via a function pointer (10 answers)
I have two classes:
typedef void (*pPrintContent)(QPainter& painter, bool friendly);
class KonsolePrintManager : QWidget
{
Q_OBJECT
public:
explicit KonsolePrintManager(QWidget *parent = nullptr) : QWidget(parent){}
void printRequest(pPrintContent pContent, QWidget *parent);
And:
class TerminalDisplay : public QWidget
{
Q_OBJECT
public:
void printContent(QPainter &painter, bool friendly);
void printScreen();
In printScreen () I intend to do something like:
void TerminalDisplay::printScreen()
{
KonsolePrintManager pManager(this);
pManager.printRequest(printContent, this); #Here the error occurs
}
And the error occurs when passing the printContent () function as a parameter of printRequest.
How could I resolve this issue?
Related
This question already has answers here:
Obtaining a function pointer to a non static member function
(2 answers)
Function pointer to member function
(8 answers)
How do pointers to member functions work?
(2 answers)
Closed 7 months ago.
I'm starting to study Unreal Engine and I saw this code:
UCLASS()
class AMyPlayerController : public APlayerController
{
GENERATED_BODY()
void SetupInputComponent()
{
Super::SetupInputComponent();
InputComponent->BindAction("Fire", IE_Pressed, this, &AMyPlayerController::HandleFireInputEvent);
InputComponent->BindAxis("Horizontal", this, &AMyPlayerController::HandleHorizontalAxisInputEvent);
InputComponent->BindAxis("Vertical", this, &AMyPlayerController::HandleVerticalAxisInputEvent);
}
void HandleFireInputEvent();
void HandleHorizontalAxisInputEvent(float Value);
void HandleVerticalAxisInputEvent(float Value);
};
I've understood everything but this line:
InputComponent->BindAction("Fire", IE_Pressed, this, &AMyPlayerController::HandleFireInputEvent);
What is the fourth parameter? I'm new to C++ but I've studied the basics of C while in college. What I can't understand is the "::". I know that the "&" means that the parameter is a pointer to the memory address of MyPlayerController, but what about the double points?
I assume that I'm passing as fourth parameter the HandleFireInputEvent function of the MyPlayerController class but I would like to know more about you.
Many thanks.
The fourth argument is the address of a class member function. And that function has a name within the class scope, just as when you have to implement a class member.
This question already has answers here:
Why doesn't a const method override a non-const method in C++?
(6 answers)
virtual function const vs virtual function non-const
(3 answers)
Top-level const doesn't influence a function signature
(7 answers)
const qualifier disappears from pure virtual function [duplicate]
(1 answer)
Closed 2 years ago.
Suppose I have a method1 in the parent class and I want to override it in a subclass. Does adding const affect the overriding? For example,
int Subclass::method1(const int a)const {...}
Does this override
int Parentclass::method1(int a) {...}
correctly?
This question already has answers here:
when does a function have to return a reference in c++ [duplicate]
(5 answers)
C++: what is the advantage of references in this case?
(3 answers)
C++ return by reference what are the effects?
(2 answers)
Closed 2 years ago.
I came across the code, where in a class one of the member function returns by reference. What are the advantages of doing it like that? E.g.:
class SomeClass {
std:vector<int> some_vals;
// some functions that modify some_vals
public:
const std::vector<int>& get_some_vals() const { return some_vals; }
};
And then when the function is used:
SomeClass my_obj;
const vector<int>& some_vals = my_obj.get_some_vals();
What is the advantage of doing it like this instead of just returning std::vector?
This question already has answers here:
Function pointer to member function
(8 answers)
Class member function pointer
(3 answers)
Closed 6 years ago.
I have a class that contains two similar non-static functions, I want to assign one of them on the fly to a function pointer:
A.h:
class A{
private:
void (*funcPtr)(int);
void do_sth(int i);
void do_sth_else(int i);
}
A.cpp:
A::A(int i){
if(i == 1)
A::funcPtr = A::do_sth; // or &A::do_sth;
else
A::funcPtr = A::do_sth_else; // or &A::do_sth_else;
}
But I get such error:
error: cannot convert 'A::do_sth' from type 'void (A::)(int)' to type 'void (*)(int)'
I read multiple similar issues, But cannot apply their solutions on my problem.
These are member functions. Either make funcPtr a member pointer by qualifying it with class name, or make do_sth(), do_sth_else() static member functions. I'd suggest using & in front of function name when taking the address of it.
This question already has answers here:
Is there a way of applying a function to each member of a struct in c++?
(13 answers)
Can I loop through (public) attributes of a C++ class?
(2 answers)
Closed 7 years ago.
How to loop (foreach public variable of class in c++), I have temp_class like the following
class temp_class
{
public:
std::string name;
int age;
class2 object_class;
private:
//some code here
}
Now I need to get a list of all public variable in the class (some thing similar if we want to get a list of all the properties of a c# class, but for c++ class)
temp_class object_class;
//here these is a code to set the values for each public variable in the object
foreach(public_variable in object_class)
{
string var_name=//get the public variable name
string var_value=//get the public variable value
}
You can't. this is C++ , unmanaged code without true reflection.