Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointer to functions?

Tags:

c++

pointers

say I have

class B{   //base class

}

class A : public B{ //derived class
}

I also have a function that returns a pointer to class B

B* returnB(){
    B * object = new A; //pointer of base class allocate object of derived class
    return object;
}

now when i try to make a pointer to function B*, I get an error

B* (*randomFunction)();
randomFunction = returnB;  

Visual Studios wont compile.

1   IntelliSense: a value of type "B*(MediaFactory::*)()" cannot be assigned to an entity of type "B*(*)()" c:\Users\...\mediafactory.cpp   35
like image 216
Thuan Trinh Avatar asked May 02 '26 06:05

Thuan Trinh


1 Answers

You seem to be trying to assign a pointer to a member function of class MediaFactory into a variable that can hold a non-member function. These entities aren't compatible. Either use boost bind or change your function pointer variable to be of B* (MediaFactory::*)() type.

like image 103
SomeWittyUsername Avatar answered May 04 '26 20:05

SomeWittyUsername



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!