A Query about Function Pointers

Posted on 2005-04-20
Last Modified: 2010-04-17
In my class 'galGeneticAlgorithm', I have the following definition for a function pointer:

bool (galGeneticAlgorithm::*generateChromosomeData)(galGenome*);

Now, I would like to write a function in a completely seperate module, and then attach the function pointer in the class to the new function that I've made elsewhere. I have defined the function thusly, and naturally (it's simple) it compiles OK;

bool tempFunction (galGenome* g) { return true; }

However, a few lines later, I try to assign the function pointer in one instance of galGeneticAlgorithm. Here is the code I am using;
test = new galGeneticAlgorithm(1000);
test->generateChromosomeData = &tempFunction;

Naturally, I get a horrid error telling me I can't do this. Here is said error;
error C2440: '=' : cannot convert from 'bool (__cdecl *)(galGenome *)' to 'bool (__thiscall galGeneticAlgorithm::* )(galGenome *)'

I understand that the function is defined as a 'standard function', and thus - isn't part of the correct class, but I don't know how to fix this. Please advise on how I can get this to be accepted, not only by Microsoft's Visual C++ .NET, but by all compilers (__thiscall is MSVC specific).
Question by:Telumehtar
    LVL 9

    Accepted Solution

    You need to change the declaration to this:

    bool (*generateChromosomeData)(galGenome*);

    What you have used declares a pointer to a method of the galGeneticAlgorithm class. However, tempFunction is a global function. Thus the error message you were getting.

    LVL 2

    Author Comment

    That's fantastic - thanks.

    Just out of curiousities sake - is there any way that I could define a method, so that I could make use of the 'this' variable? I don't think I'll need to in this case, but there are a couple of places I can think of where it would be 'neat'.
    LVL 9

    Expert Comment

    You could pass 'this' as a parameter to the global function - i.e. declare it like so:

    bool tempFunction(galGenome*, galGeneticAlgorithm*);

    LVL 2

    Author Comment

    Of course - yup, that would work great.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    A short article about problems I had with the new location API and permissions in Marshmallow
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now