Thuan Trinh
Thuan Trinh

Reputation: 75

pointer to functions?

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

Upvotes: 3

Views: 9145

Answers (2)

SomeWittyUsername
SomeWittyUsername

Reputation: 18348

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.

Upvotes: 2

justin
justin

Reputation: 104698

You are assigning a pointer-to-member function to a function pointer.

A method of your class, unless static, has a different signature and approach to calling. The simple function declaration does not account for class representations (e.g. implicit this).

See here: http://www.parashift.com/c++-faq/fnptr-vs-memfnptr-types.html

Upvotes: 0

Related Questions