C++ function pointer (class member) to non-static member function - c++

class Foo {
public:
Foo() { do_something = &Foo::func_x; }
int (Foo::*do_something)(int); // function pointer to class member function
void setFunc(bool e) { do_something = e ? &Foo::func_x : &Foo::func_y; }
private:
int func_x(int m) { return m *= 5; }
int func_y(int n) { return n *= 6; }
};
int
main()
{
Foo f;
f.setFunc(false);
return (f.*do_something)(5); // <- Not ok. Compile error.
}
How can I get this to work?

class A{
public:
typedef int (A::*method)();
method p;
A(){
p = &A::foo;
(this->*p)(); // <- trick 1, inner call
}
int foo(){
printf("foo\n");
return 0;
}
};
void main()
{
A a;
(a.*a.p)(); // <- trick 2, outer call
}

The line you want is
return (f.*f.do_something)(5);
(That compiles -- I've tried it)
"*f.do_something" refers to the pointer itself --- "f" tells us where to get the do_something value from. But we still need to give an object that will be the this pointer when we call the function. That's why we need the "f." prefix.

class A {
int var;
int var2;
public:
void setVar(int v);
int getVar();
void setVar2(int v);
int getVar2();
typedef int (A::*_fVar)();
_fVar fvar;
void setFvar(_fVar afvar) { fvar = afvar; }
void insideCall() { (this->*fvar)(); }
};
void A::setVar(int v)
{
var = v;
}
int A::getVar()
{
std::cout << "A::getVar() is called. var = " << var << std::endl;
return var;
}
void A::setVar2(int v2)
{
var2 = v2;
}
int A::getVar2()
{
std::cout << "A::getVar2() is called. var2 = " << var2 << std::endl;
return var2;
}
int main()
{
A a;
a.setVar(3);
a.setVar2(5);
// a.fvar = &A::getVar;
a.setFvar(&A::getVar);
(a.*a.fvar)();
a.setFvar(&A::getVar2);
(a.*a.fvar)();
a.setFvar(&A::getVar);
a.insideCall();
a.setFvar(&A::getVar2);
a.insideCall();
return 0;
}
I extended Nick Dandoulakis's answer. Thank you.
I added a function which set the member function pointer from outside of the class. I added another function which can be called from outside to show inner call of member function pointer.

Try (f.*do_something)(5);

#include<iostream>
using namespace std;
class A {
public:
void hello()
{
cout << "hello" << endl;
};
int x = 0;
};
void main(void)
{
//pointer
A * a = new A;
void(A::*pfun)() = &A::hello;
int A::*v1 = &A::x;
(a->*pfun)();
a->*v1 = 100;
cout << a->*v1 << endl << endl;
//-----------------------------
A b;
void(A::*fun)() = &A::hello;
int A::*v2 = &A::x;
(b.*fun)();
b.*v2 = 200;
cout << b.*v2 << endl;
}

I think calling a non static member of the class could also be done using a static member function.

Related

How to invoke function pointer, through another pointer type?

Question might be a bit confusing; here's the problem:
I have this:
class FunctionContainer
{
void* functionPointer;
}
void Test()
{
cout << 'a';
}
int main()
{
FunctionContainer* f = new FunctionContainer();
f->functionPointer = &Test;
f->functionPointer;
}
My problem here is that I can't invoke the function like this, it just skips the line. What am I doing wrong here?
The correct way is this:
class FunctionContainer
{
public:
void (*functionPointer)(void);
}
void Test()
{
cout << 'a';
}
int main()
{
FunctionContainer* f = new FunctionContainer();
f->functionPointer = &Test;
f->functionPointer();
}
Where
returnType (*variableName)(argumentType1, argumentType2, argumentType3...);
And (in this example)
returnType name = f->variableName(argument1, argument2, argument3...);

How to initialise static variables of class A in class B?

I tried using a static func in class A.
class A {
private:
static int a, b;
static void init(void) {
a = 1, b=0;
}
};
class B {
private:
A::init();
}
It is giving non-friend class cannot have a qualified name.
The following works:
class A {
private:
static int a;
public:
static void initialize();
void addStatic();
void getA();
};
class B {
public:
void initializeClassA();
};
int A::a = 10;
void A::addStatic() {
++a;
}
void A::getA() {
return a;
}
void A::initialize() {
a = 0;
}
void B::initializeClassA() {
A::initialize();
}
int main() {
A a;
B b;
std::cout << "Before Adding - " << a.getA() << std::endl;
for(int i = 0; i < 3; ++i)
{
a.addStatiic();
}
std::cout << "After Adding - " << a.getA() << std::endl;
b.initializeClassA();
std::cout << "After Reinitializing - " << a.getA() >> std::endl;
return 0;
}

C structure object member name as parameter

How can I pass a structure name, object name, different member name(whose value to be verified) as a parameter to a function ?
struct st{
int a;
int b;
}
bool verify(____ st , ____ b){
if(obj.b == 5)return true;
return false;
}
int main(){
st obj;
// now that I know all the names of members of struct name
// HOW can I verify passing different member name as parameter
cout<<verify(__,__);
}
For an instance:
(example with error)
#include<iostream>
using namespace std;
struct st{
int a;
int b;
};
bool verify(st obj, st.a val){
if(obj.a==val)
return true;
}
int main()
{
cout<<"Hello World"<<endl;
st obj;
cout<<verify(obj,a);
//cout<<verify(obj,b);
return 0;
}
You can use a pointer to data member:
bool verify(const st &obj, int st::* field) {
return obj.*field == 5;
}
...
st obj;
std::cout << verify(obj, &st::a);
std::cout << verify(obj, &st::b);
But the syntax is confusing and just passing a reference to the data member to verify is easier:
bool verify(int field) {
return field == 5;
}
...
st obj;
std::cout << verify(obj.a);
std::cout << verify(obj.b);
Using a template:
#include <iostream>
struct st
{
int a;
int b;
};
struct st2
{
int c;
int d;
};
template<typename T>
bool verify(const T &obj, int T::* field)
{
return obj.*field == 5;
}
int main()
{
st obj = {5, 6};
st2 obj2 = {5, 6};
std::cout << verify(obj, &st::a) << "\n"; // 1, since obj.a is 5
std::cout << verify(obj2, &st2::d) << "\n"; // 0, since obj.d is not 5
return 0;
}

Multiple functions for a class does not store previous results

Problem
I want to create a function inside a class which function2 will use the result generated from function1. I have a small code snippet where I tried to make it easy to understand.
#include <stdio.h>
class GreaterSmaller {
public:
int greater, smaller;
};
GreaterSmaller findGreaterSmaller(int a, int b)
{
GreaterSmaller s;
if (a > b) {
s.greater = a;
s.smaller = b;
}
else {
s.greater = b;
s.smaller = a;
}
return s;
}
GreaterSmaller print()
{
GreaterSmaller s;
std::cout << s.greater << s.smaller << std::endl;
}
int main()
{
int x = 4;
int y = 3;
GreaterSmaller result;
result = findGreaterSmaller(x, y);
result = print(); // I want it to print 4 & 3
return 0;
}
P.s Just wanted to mention I am not trying to print the result in the function2 I have created that for a demo.
Define the second method as taking an argument of the first type, and pass it when you call it, as such:
void print(GreaterSmaller &s)
{
std::cout << s.greater << s.smaller << std::endl;
}
print(result); // I want it to print 4 & 3

How to access a dynamic object property not using getter method?

#include <iostream>
class SomeClass
{
public: int *SomeNumber;
SomeClass() { SomeNumber = new int; *SomeNumber = 5; }
~SomeClass() { delete SomeNumber; }
int getSomeNumber(void) { return *SomeNumber; }
};
int main()
{
SomeClass A;
std:: cout << A.getSomeNumber() << std::endl; // outputs 5
std:: cout << A.SomeNumber << std::endl; // outputs SomeNumber address
return 0;
}
How can I get *SomeNumber, not its address, by not using the method getSomeNumber()? If SomeNumber were not a pointer to a int, I could get it with A.SomeNumber
Sorry If I were not clear enough.
Thanks in advance.
Simple:
*A.SomeNumber
It works because . has higher precedence than *, so it's the same as
*(A.SomeNumber)
Can't you just do:
std:: cout << (*A.SomeNumber) << std::endl;
Avoid making your properties public!!!!
You could use visitor design pattern instead of this code like:
class SomeClass
{
public: int *SomeNumber;
SomeClass() { SomeNumber = new int; *SomeNumber = 5; }
void visit( IVisitor* visitor ){ visitor->doSomething(*SomeNumber);}
~SomeClass() { delete SomeNumber; }
int getSomeNumber(void) { return *SomeNumber; }
};
IVisitor is an interface you can implement it and anything you want.