Disallow private constructor invocation in friend function

Posted by user2907032 on Stack Overflow See other posts from Stack Overflow or by user2907032
Published on 2013-10-24T21:37:37Z Indexed on 2013/10/24 21:54 UTC
Read the original article Hit count: 190

Filed under:
|
|
|

Is there any way to not allow private construction in friend function, In case we do have private constructor with friend function in our class. Only Static method should be responsible for object creation and other than this compiler should flash error message

#include <iostream>
#include <memory>
using namespace std;

class a
{

public:
    void see ()
    {

    cout<<"Motimaa";

    }
    static a& getinstance()
    {
        static a instance;
        return instance;
    }

private:
    a() {};
    friend void access();
};
void access ()
{
a obj;
obj.see();//still friend function can access
}

int main()
{

a::getinstance().see();

access();
return 1;

}

© Stack Overflow or respective owner

Related posts about c++

Related posts about constructor