Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

returning pointer to a function inline without typedef

I’m running into syntax errors with C++ where I have to return a pointer to a function inline.

struct Note{}

Observer.hpp

class Observer {
    protected:
        void (*notify)(Note *note); // should this be *(*notify...)?
    public:
        void (*(*getNoteMethod)())(Note *note);
};

Observer.cpp

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

void (*Observer::getNoteMethod())(Note*) { //error: non-static data member defined out-of-line
    return this->notify;
}

I’m getting this error, error: non-static data member defined out-of-line

I’m new to C++ and attempting to define the return function signature correctly.

>Solution :

Declare the member function as:

class Observer {
    protected:
        void (*notify)(Note *note);
    public:
        void (*getNoteMethod())(Note *note);
};

Better to declare the function pointer type in advance via using or typedef. E.g.

class Observer {
    using function_pointer_type = void(*)(Note*);
    protected:
        function_pointer_type notify;
    public:
        function_pointer_type getNoteMethod();
};
Observer::function_pointer_type Observer::getNoteMethod() {
    return this->notify;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading