Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

member function pointer in constructor

Tags:

c++

I tried to use std::shared_pointer with deleter. I tried to use a member function as the deleter. However it could not compiled. Compiler gave me a error message but I could not understand why it did not work. Does someone knows why it did not work? Thank you very much.

Simplified code is following,

#include <memory>

class MemberFunctionPointerInConstructor {
public:
    MemberFunctionPointerInConstructor(void) {
        std::shared_ptr<int> a = std::shared_ptr<int>(new int(1), deleter);  // this line makes a compiler error message
    }

    void deleter(int* value) {
        delete value;
    }
};

The error message from compiler is following,

error: invalid use of non-static member function
std::shared_ptr<int> a = std::shared_ptr<int>(new int(1), deleter);
                                                                 ^

Thank you very much.

like image 467
mora Avatar asked Mar 04 '16 15:03

mora


People also ask

Can you get a function pointer to a constructor?

The standard somewhat ominously says that constructors have no names. As a consequence, I think, you cannot obtain a function pointer to a constructor. But you can write functions that construct objects. E.g., ... Then you could pass &create<MyType,MyArgas a function pointer. However,

What is the purpose of the body of a constructor?

Here is something that always works: the {body} of a constructor (or a function called from the constructor) can reliably access the data members declared in a base class and/or the data members declared in the constructor's own class.

Could a pointer to any other function be used in OPOP?

OP has different requirements, he would need to tell us. could a pointer to any other function. No. What the OP asked is this: as argument to a function? the question he asked would be moot). Moreover, he provided some constructor. The objects should all inherit from a base class. beforehand, how many should be created. pointer to a constructor.

Is there a way to call a function from a constructor?

>There is also no way of "calling a function" either. All that one). And your point is? The standard speaks of a function call operator, and a function call expression. The expression evaluates the arguments and calls the function. (Which function other things.) The standard doesn't have a constructor call expression. It expression.


3 Answers

To use a member function that isn't bound to an instance of your class you'd have to declare the method static

static void deleter(int* value) {
    delete value;
}
like image 122
Cory Kramer Avatar answered Oct 19 '22 22:10

Cory Kramer


If you want to use a non-static member function as a deleter, you have to bind it to an instance—but note that the instance would need to still be alive when the deleter is invoked. For example,

class ShorterName {
public:
    ShorterName(void) {
        using namespace std::placeholders;
        auto a = std::shared_ptr<int>(new int(1),
                   std::bind(&A::deleter, this, _1));
    }

    void deleter(int* value) {
        delete value;
    }
};

If you don't need a particular instance, you can make the function static, so it doesn't require an instance.

class ShorterName {
public:
    ShorterName(void) {
        auto a = std::shared_ptr<int>(new int(1), deleter);
    }

    static void deleter(int* value) {
        delete value;
    }
};
like image 37
Yam Marcovic Avatar answered Oct 19 '22 22:10

Yam Marcovic


There are several ways to solve this. If you actually mean a non-static member function, one way of doing so (not the only one) would be through a lambda function:

class MemberFunctionPointerInConstructor {
public:
    MemberFunctionPointerInConstructor() {
        std::shared_ptr<int> a = std::shared_ptr<int>(
            new int(1), 
            [this](int *p){this->deleter(p);});  
    }   

    void deleter(int* value) {
        delete value;
    }   
};  
like image 31
Ami Tavory Avatar answered Oct 19 '22 22:10

Ami Tavory