This question already has answers here:
Function pointer to member function
(8 answers)
Closed 4 years ago.
class Person;
class Command {
private:
Person * object;
//what is this? - a functor?...
void (Person::*method)();
public:
Command(Person *a, void(Person::*m() = 0): object(a),method(m) {}
void execute() { (object->*method(); }
};
// defining class Person here
class Person {
private:
string name;
Command cmd;
public:
Person(string a, Command c): name(a),cmd(c) {}
void talk();
void listen();
};
I was wondering what line 6 here means? Is that a way of defining a function or a functor? Also where does this type of function def appear and tends to be used? I found this example Under "Design Patterns" within Object Oriented Programming - this approach type is called Behavioural Command Pattern.
This is a pointer to Person class member taking no parameters and returning void.
Such initialization allows class Command referring to custom methods of class Person. Imagine class Person to be defined like this:
class Person
{
public:
void f1() { std::cout << "Call f1\n"; }
void f2() { std::cout << "Call f2\n"; }
};
Then we can create Command objects in this way:
Person p;
Command c1(&p, &Person::f1);
Command c2(&p, &Person::f2);
Executing these commands calls corresponding methods of given person:
c1.execute(); // prints "Call f1"
c2.execute(); // prints "Call f2"
This code line void (Person::*method)(); is a function pointer. void means that the function that is pointed won't return any data. (Person::*method)() is the definition of the pointer, the pointer will point to a void function.
In this page you can see how the syntax is:
Function Pointer Syntax
The syntax for declaring a function pointer
might seem messy at first, but in most cases it's really quite
straight-forward once you understand what's going on. Let's look at a
simple example:
void (*foo)(int);
In this example, foo is a pointer to a function taking one argument,
an integer, and that returns void. It's as if you're declaring a
function called "*foo", which takes an int and returns void; now, if
*foo is a function, then foo must be a pointer to a function. (Similarly, a declaration like int *x can be read as *x is an int, so
x must be a pointer to an int.)
The key to writing the declaration for a function pointer is that
you're just writing out the declaration of a function but with
(*func_name) where you'd normally just put func_name.
Related
This question already has answers here:
Calling C++ member functions via a function pointer
(10 answers)
Closed 10 months ago.
Let's say that I have the function
doSomething(char* (*getterOne)())
{
//do something here
}
although I've named the parameter of doSomething "getterOne", I have no way of verifying that the function is going to be a getter ( I think? ). So I wonder, is there a way to explicitly specify the kind of function that can be passed as an argument? For example, if I have the class "Cat", is there a way to say that the function doSomething accepts as parameter function, that belongs to the class Cat, and if so, how can I use it like this:
doSomething(char* (*getterOne)())
{
Cat cat;
cat.getterOne(); // where getterOne will be the getter that I pass as a parameter and do something with it
}
Also if anyone asks why I use char* instead of string, it is for a school project and we are not allowed to use string.
A function pointer and a pointer-to-member-function are two different things. If you make a function that accepts a pointer-to-member-function you have no choice but to specify which class it belongs to.
Here is an example.
#include <iostream>
struct Cat {
char* myGetter() {
std::cout << "myGetter\n";
return nullptr;
}
};
void doSomething(char* (Cat::*getterOne)())
{
Cat cat;
(cat.*getterOne)();
}
int main() {
doSomething(&Cat::myGetter);
}
The syntax for using a pointer-to-member is .* or ->* and the extra brackets are needed.
This question already has answers here:
Function with same name but different signature in derived class not found
(2 answers)
Closed 2 years ago.
I am passing different types of argument to the fun(), the code gets compiled, but still, the same function that prints "Class B" is getting executed. Shouldn't there happen overloading of functions?
Why does this happen?
#include <iostream>
using namespace std;
class A
{
public:
void fun(int x)
{
cout<<"class A"<<endl;
}
};
class B: public A
{
public:
void fun (char c)
{
cout<<"Class B"<<endl;
}
};
int main()
{
B obj;
obj.fun('c');
obj.fun(3);
}
B::fun() shadows A::fun() and an int literal is easily converted to char so your compiler had no difficulty with resolving that call. You can add using A::fun; to B's definition to make it visible.
When writing obj.fun(<argument>), first what fun to be called will be looked up. Since the compiler only looks at B first, it finds B::fun(char) both times, which is what is selected.
It would only look at A if there was no one parameter member function, so fun in B effectively hides fun in A. To overcome that, you can directly call A's fun like:
obj.A::fun('c'); // A
static_cast<A&>(obj).fun('c'); // A
Or you can bring A::fun(int) into B by using it:
class B: public A
{
public:
using A::fun;
void fun(char c)
{
std::cout << "Class B\n";
}
};
obj.fun('c'); // B
obj.fun(3); // A
You cannot overload like this across a heirarchy.
Name lookup happens before overload resolution and access control.
Once a function name is found in B, other classes up the heirarchy will not be checked. So all the candidate functions for a particular function call would be the functions present in B.
You will have to make the function name in A visible in B by saying:
using A::foo;
inside class B.
Now the overload will work as you expect.
This question already has answers here:
virtual function default arguments behaviour
(7 answers)
Closed 9 years ago.
the below is my test code, i think it will output "Der:12", but the result is "Der:11", any one can tell me why output this, and where is the default argument store?
#include <stdio.h>
class Base{
public:
virtual void show(int i = 11)
{
printf("Base:%d\n", i);
}
};
class Der : public Base{
public:
virtual void show(int i = 12)
{
printf("Der:%d\n", i);
}
};
int main()
{
Base *p = new Der();
p->show();
return 0;
}
Hmmm, I'm not sure it's actually valid to override a virtual function with a different default parameter, and it's certainly not sensible. But on the other hand, the compiler is doing the right thing, even if it's contrary to your expectations.
Base *p;
p->show();
What happens here is that the compiler looks into Base for a function taking no arguments. There isn't one, but it finds the one-argument function and calls show(int) with the default parameter of 11.
But the function is virtual, and so because p's dynamic type is Der, it's Der::show(int) that actually gets called -- but crucially, still with Base's default argument of 11, but the default argument is looked up statically, not using run-time dispatch.
I haven't tried it, but I'd imagine if you said
Der *p = new Der();
p->show();
you'd get 12 output instead.
This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 10 years ago.
I cannot do this:
class A
{
public:
A()
{
}
};
A a1();
Because A a1(); looks like a function prototype.
But I can do this:
class B
{
public:
B(std::string argument)
{
std::cout << argument;
}
};
B b1("Text");
These two things are essentially the same except the compiler is able to distinguish B b1("Text"); as NOT being a function prototype, because some data is passed in the parenthesis.
Is there any reason why the brackets must be omitted for A, or is the reason because the compiler thinks it is a function definition?
That's exactly it, and it's known as most vexing parse. The reason is that if A a1(); was treated as an object declaration, you wouldn't be able to declare a function with that prototype. And you want to be able to declare a function, right?
B b1("Text"); works because it can't be treated as a function prototype, but, for example, B b(A()); can and will.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does void(U::*)(void) mean?
Considering the following:
template <class T>
class myButtoncb {
private:
T *ptr;
void (T::*cback) (void)
}
What I understand is:
void (*cback) (void)
Which is nothing but a function pointer that to a function that returns void, and takes no argument.
What I dont understand is, what is the importance of T::? Isn't it enough to declare
only like void (*cback) (void) ?
This says that it's a member function that has a this pointer. Otherwise, it would be a free function, wouldn't have any idea what object it was operating on, and wouldn't be able to access any non-static member functions or member variables.
From C++ FAQ
Is the type of "pointer-to-member-function" different from "pointer-to-function"?
Yep.
Link which I've provided to you has a lot of information about this topic.
The function, you pass there, must be declared inside the class T - the template parameter of myButtoncb. So you can use a function like the following:
class A
{
public:
void foo(void);
};
myButton<A> b;
b.cback = &A::foo;