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.

using namespace std;

class a

    void see ()


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

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

int main()


return 1;


Recommended Answers

All 4 Replies

A friend can see private members. If you don't want to allow that then you'd have to work through the public or protected interface rather than friendship.

Could you elaborate the same with example.

Can you provide a realistic example of the behavior you want to achieve?

i can't provide on this forum because it is part of my project and confidential.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.