5

How I declare B's constructor to be a friend of A? I tried:

class A
{
  private:
   A();
  public:
   friend B::B();
};

class B
{
  public:
    B();
};
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
IamMan
  • 322
  • 1
  • 2
  • 13
  • 4
    Just edited it so it wouldn't be closed. Some trigger happy closers there -- give some slack to potential non-native speaker or newbies please. @IamMan, welcome to StackOverflow -- try to make questions clear and indent code properly or else the question will get closed if a lot of us don't understand it. Also, if someone answers the question correctly, click the check mark to the left it to indicate that it's the correct answer. – Lou Franco Dec 04 '10 at 20:36

1 Answers1

7

replace B:: with class;

class A
{
private:
    A();
public:
   friend class B;
};

class B
{
public:
    B();
};
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
  • 6
    This answer actually makes ALL of B a friend. It's probably what the OP wanted, but not what they asked for. What they asked for is impossible. Other people searching for an answer to this question should be made aware. – Edward Strange Dec 04 '10 at 22:27
  • 1
    Yeah. This answer should be modified to make it clear that the suggestion doesn't do exactly what the question was asking --- because what the question wants isn't possible. If someone were to apply this answer, they may not think carefully about the fact that they're granting much broader access than the original question asked for. (To take the idea to the extreme: Another solution would be to make the private members of `A` public. This would also grant the desired access, but again, it would be granting much more access than the OP was looking for, which should be pointed out.) – Adam H. Peterson Sep 04 '15 at 19:13