use callback function in class

I try to write code like C callback in class
but compile tell me, why??

error: 'dInv' was not declared in this scope|

1
2
3
4
5
6
7
8
class StockStory{
   private:
      bool CompareDay(bool (*)(const double*));
};

bool StockStory::CompareDay(bool (*PriceMatch)(const double* dInv)){
      return (*PriceMatch)(*dInv);
}
Last edited on
You'd have to pass the function pointer and the const double* variable to CompareDay(), here's a stylized example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <random>
#include <chrono>
#include <cmath>

using FunctionPtr = bool(*)(const double* dInv);

bool positiveNum (const double* dInv)
{
    return *dInv > 0;
}
bool CompareDay (FunctionPtr f, const double* dInv)
{
    return (!f(dInv));
}

int main()
{
    std::default_random_engine numGenerator(static_cast<unsigned int>(time(NULL)));

    std::uniform_real_distribution<double> range(-100, 100);

    const double* dInv = new double(range(numGenerator));

    FunctionPtr f = &positiveNum;

    std::cout << "The random number is: " << *dInv << '\n';

    std::cout << CompareDay(f, dInv) << '\n';

    delete dInv;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>

class clsA{
   public:
   bool funA( bool (*funB)(int i))
   {
       return *funB;
   }
};

class clsB:public clsA {
	public:
	   bool funB_From_ClassB (int);
	   bool Caller();
};

bool clsB::funB_From_ClassB(int ii){
	return false;
};

bool clsB::Caller(){
   int iii;
   return funA(&funB_From_ClassB(iii));//How can I get the address of funB_From_ClassB??
};


int main()
{
   clsA Aclass;
   clsB Bclass;
   printf("over\n");
}

I try to use the way you toll me, but same problem... thansk!
1
2
3
4
5
6
7
bool clsB::Caller(){
   int iii;
   using ptr_funB = bool(clsB::*)(int);
   ptr_funB f = &funB_From_ClassB;
   return (this->*f)(iii);
  // return funA((iii));//How can I get the address of funB_From_ClassB??
};

http://stackoverflow.com/questions/16276373/how-to-call-member-function-through-member-function-pointer
#include <stdio.h>
#include <chrono>
#include <cmath>

class clsB;

using ptr_funB = bool(clsB::*)(int);

class clsA{
public:
bool funA( bool (*funB)(int i))
{
printf("have to go there\n");
return *funB;
}
};

class clsB:public clsA {
public:
bool funB_From_ClassB (int);
bool Caller();
};

bool clsB::funB_From_ClassB(int ii){
return false;
};

bool clsB::Caller(){
int iii;

ptr_funB f = &funB_From_ClassB;
return (this->*f)(iii);
};


int main()
{
clsB Bclass;
Bclass.Caller();

printf("over\n");
}

thanks , but I want to print out is "have to go there",
if I try to use something like using ptr_funA = bool(clsA::*)(int);
then compiler will told me type not match.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>

class clsA;
class clsB;


//using ptr_funB = bool(clsB::*)(int);
using ptr_funA = void(clsA::*)();

class clsA
{
    public:
   // void funA( bool (*funB)(int i))
   void funA()
    {
        std::cout << "have to go there\n";
        //return *funB;
    }
};
class clsB : public clsA
{
    public:
    bool funB_From_ClassB (int);
    void Caller();
};

bool clsB::funB_From_ClassB(int ii){
return false;
};

void clsB::Caller()
{
    int iii;
  //  ptr_funB f = &funB_From_ClassB;
    ptr_funA fA = &(clsA::funA);
//    return (this->*f)(iii);
     (this->*fA)();
};


int main()
{
clsB Bclass;
Bclass.Caller();

//printf("over\n");printf() is C, not C++
std::cout << "over \n";
}
#include <iostream>
#include <chrono>
#include <cmath>

class clsA;
class clsB;


using ptr_funB = bool(clsB::*)(int);
//using ptr_funA = void(clsB::*)(int);

class clsA
{
public:
void funA( bool (clsB::*)(int i))
{
//for (int j=i;i<=0;i--)//Need i to do something
std::cout << "have to go there\n";
}
};
class clsB : public clsA
{
public:
int ii=3;
bool funB_From_ClassB (int);
void Caller();
};

bool clsB::funB_From_ClassB(int ii){
return false;
};

void clsB::Caller()
{
ptr_funB fB = &(clsB::funB_From_ClassB);
funA(fB);
};


int main()
{
clsB Bclass;
Bclass.Caller();
std::cout << "over \n";
}
Thanks solve many problem for me, thanks.
another question is : can class A get the code and parameter together?
C++ seem always ignore the parameter pass from class B.
can class A get the code and parameter together?

sorry, I don't understand what you mean by this. and please go back to using code-tags if you're posting code
Thanks for your responses and patience!
the function of class A like qsort(), and class B like compare function,
I want to pass a ptr to class A, many way can do it.
A have to loop the function from class B.
Now I solve it by some thing like EOF(end of file) at last binary codes struct, because class B do not
known when should stop. If anyone known the method, please help me, and last.. sorry about my
English.
Last edited on
I want to pass a ptr to class A, ... A have to loop the function from class B.
Since A is a base class of B one way to do this is to declare the function in common as virtual in the base class and call it's derived class implementation through a pointer to base instantiated on a derived object:
http://stackoverflow.com/questions/9974868/c-call-derived-function-from-base-class-instance
thanks I've fix my logic,but now i cannot call fb in funA.

class clsB;

using ptr_funB = bool(clsB::*)(int);

class clsA
{
public:
int ia=5;

void funA(int ib, ptr_funB fb)
{
for(int i=ia;i<ib;i--)
{
bool ret = fb(ib);//How to call fb?
return ret;
}

}
};

class clsB : public clsA
{
public:
int ib=3;
bool funB(int);
void Caller();
};

bool clsB::funB(int ib){
return false;
};

void clsB::Caller()
{
ptr_funB fB = &(clsB::funB);
funA(ib,fB);
};


int main()
{
clsB Bclass;
Bclass.Caller();
std::cout << "over \n";
}
since B is derived from A it has access to A's methods but not the other way round. The only way to access B's methods is through A*
thank you, I am up side down now ha! ha! over
You must the function as a pointer, otherwise it won't except it.
This kind of errors are spotted easily once you will develop more and more with c++ so don't worry about it.
If you're still having troubles doing that you can use a program such as checkmarx to detect vulnerabilities among your code and fix them.
Good luck!
Ben.
understood thank you, the code call self back .
Topic archived. No new replies allowed.