Hi,

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;

}

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.

Hi,
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, networking, learning, and sharing knowledge.