This question already has an answer here:
About meaning of ::* on this code [duplicate]
(1 answer)
Closed 5 years ago.
While refactoring a piece of code I came across the line below:
class Bar
{
protected:
int (Bar::* fooFunction)(float); //this line
}
I have never seen this kind of syntax before. What is this syntax and why is it used it used in C++?
It's a member function pointer.
Specifically, it's a pointer to a member function of a Bar object that takes a float argument and returns a int.
Read more here: http://en.cppreference.com/w/cpp/language/pointer#Pointers_to_member_functions
What is this syntax
It declares a pointer to member function.
why is it used it used in C++?
It is used to point to non-static member functions.
Related
This question already has answers here:
How can I pass a member function where a free function is expected?
(9 answers)
How can I pass a member function pointer into a function that takes a regular function pointer?
(4 answers)
Closed 9 months ago.
I'm having problems casting a function pointer.
It works (no cast needed) outside a class.
Here is the signature of the function I'm calling.
Result* FancyClass::callMe(void(*functionPointer)())
It works with.
void defaultState()
{
//
}
// ..
Result *result= instance.callMe(defaultState);
But it does not work with
void MyClass::defaultState()
{
//
}
// ..
Result *result= instance.callMe(MyClass::defaultState);
I am getting this:
argument of type "void (MyClass::)()" is incompatible with parameter of type "void ()()"
How to cast this correctly?
You can't cast it correctly, because pointers to member functions are different from pointers to regular functions. For starters, you must write &MyClass::defaultState, and the parameter has to be of type void(*MyClass::functionPointer)()
You can't even store &MyClass::defaultState directly in a std::function<void()>. What object would you call it on? But you could bind an instance, and store the bound result in a std::function<void()>.
The question was tagged "C", but C doesn't understand classes and can't call member functions.
This question already has answers here:
Passing references to pointers in C++
(10 answers)
Closed 4 years ago.
I have two function signatures in C++
void printArray(int* arrayPtr);
void printArray(int*& arrayPtr);
I understand the 1st function. It says the function takes in a arrayPtr argument which is of type that's a pointer pointing to an integer.
Both function signature works, but I have a hard time understanding the 2nd signature(*&) and what benefits it offers?
It's exactly the same as type versus type&; the first is a value and the second is a reference. The fact that type is a pointer doesn't change that.
This question already has answers here:
Typedef function pointer?
(6 answers)
Closed 6 years ago.
I ran across a line of code that looks like the following:
typedef Foo* (*CREATE_BAR)(uint32_t);
How exactly does this work? What is happening in this code?
It's a function pointer type named CREATE_BAR which accepts a uint32_t argument and returns a Foo*. It could hold a pointer to any such function.
It is a type for a pointer on function returning Foo*, and taking uint32_t
In c++11, it would be
using CREATE_BAR = Foo* (*)(uint32_t);
This question already has answers here:
How does dereferencing of a function pointer happen?
(5 answers)
Closed 6 years ago.
Suppose I have the following types:
typedef uint8 (*P2MyFunc)(void);
typedef struct
{
P2MyFunc ptr;
}MyStruct;
Given an instance inst of MyStruct. is there any difference at all between the following calls ?
(*inst.ptr)();
inst.ptr();
Both seem to work just fine but the first one might be prone to compiler warnings.
They mean the exact same thing according to the C Standard. You could even go a step further. The following below give the same result:
(********inst.ptr)();
inst.ptr();
This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 8 years ago.
I saw something like:
const char& operator[] (int Index) const
the first const I understand. It's to protect the returning char from being modified.
But what does second const mean? Why do we sometimes use two const, sometimes just one?
It can be used for any member function, not only operators. It means, that this function will:
not be able to modify any data members (except mutable ones)
will not be able to call any non-const member functions