in CPP is there any concept of friend constructor

Apr 29, 2015 at 10:10am
Is there any friend constructor concept in CPP. If yes then plz tell me about it.
Apr 29, 2015 at 10:52am
No, it wouldn't make any sense.
Apr 29, 2015 at 10:55am
There is Factory design pattern. Is that close, conceptually?
Apr 29, 2015 at 10:56am
I don't know exactly what you mean...
Do you mean that only the constructor can be invoked by a friend from any of its methods;
or do you mean that only a certain function in a friend can invoke the constructor?

The first thing does NOT exist, a friend Method/class allways sees everything inside the class.
The second thing works partialy, but this method can see everything in the class (which makes sense)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class A
{
public:
    A();
};
class F
{
    F() {}
    int i; // also visible from A::A()
public:
    friend A::A();
};
A::A() {
  F f;
  f.i = 20;
}
Last edited on Apr 29, 2015 at 10:58am
Apr 30, 2015 at 9:32am
I just want to know that , in cpp there is a concept of friend function, just as if there any concept of friend constructor or not.
Apr 30, 2015 at 10:26am
Does the example shown in my last post satisfy your needs?
Apr 30, 2015 at 11:04am
A constructor is a (special) function so you can declare a constructor as friend just like you can with normal functions. The same could be said about operators.
Topic archived. No new replies allowed.