0

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;

}

Edited by can-mohan: It was uncomplete

2
Contributors
4
Replies
22
Views
4 Years
Discussion Span
Last Post by can-mohan
0

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.