By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,143 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,143 IT Pros & Developers. It's quick & easy.

friend default constructor

P: n/a
mc
Hello,

I'm trying to create a class (A) with a default constructor friend to
another one (B) as follows:

class A
{
public:
A(int a);
~A();

private:
A(); // default constructor
}

class B
{
// must be able to use A::A()
}

What I need is that only A and B can use the private default constructor
(A::A()). By adding a "friend class B;" in class A, I got it to work but I
do not want B to be able to access the whole of A. Is there a way to do
this? Thank you in advance.

Regards,

MC

Jul 10 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
mc wrote:
Hello,

I'm trying to create a class (A) with a default constructor friend to
another one (B) as follows:

class A
{
public:
A(int a);
~A();

private:
A(); // default constructor
}

class B
{
// must be able to use A::A()
}

What I need is that only A and B can use the private default constructor
(A::A()). By adding a "friend class B;" in class A, I got it to work but I
do not want B to be able to access the whole of A. Is there a way to do
this? Thank you in advance.
You need a proxy friend (or a friend proxy). There is no direct way, I
believe, you would have to rely on copy-construction (and RVO).

class A
{
public:
class defaultAmaker
{
// everything is private
static A makeA() { return A(); }
friend class B;
};

friend class defaultAmaker;
...
};

class B
{
A a = A::defaultAmaker::makeA(); // constructs A using A::A()
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 10 '08 #2

P: n/a
mc
Thank you.

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:g5**********@news.datemas.de...
mc wrote:
>Hello,

I'm trying to create a class (A) with a default constructor friend to
another one (B) as follows:

class A
{
public:
A(int a);
~A();

private:
A(); // default constructor
}

class B
{
// must be able to use A::A()
}

What I need is that only A and B can use the private default constructor
(A::A()). By adding a "friend class B;" in class A, I got it to work but
I do not want B to be able to access the whole of A. Is there a way to
do this? Thank you in advance.

You need a proxy friend (or a friend proxy). There is no direct way, I
believe, you would have to rely on copy-construction (and RVO).

class A
{
public:
class defaultAmaker
{
// everything is private
static A makeA() { return A(); }
friend class B;
};

friend class defaultAmaker;
...
};

class B
{
A a = A::defaultAmaker::makeA(); // constructs A using A::A()
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Jul 10 '08 #3

P: n/a
On Jul 10, 1:28*pm, "mc" <mc_r...@yahoo.comwrote:
Hello,

I'm trying to create a class (A) with a default constructor friend to
another one (B) as follows:

class A
{
public:
* * A(int a);
* * ~A();

private:
* * A(); * // default constructor

}

class B
{
* // must be able to use A::A()

}

What I need is that only A and B can use the private default constructor
(A::A()). *By adding a "friend class B;" in class A, I got it to work but I
do not want B to be able to access the whole of A. *Is there a way to do
this? *Thank you in advance.

Regards,

MC
you could also try this:
class B; // forward declare B but define it later

class A
{
public:
A(int a);
~A();

private:
A(); // default constructor

friend class B; // say B is a friend

}

class B // now define B
{
// must be able to use A::A()

}
Jul 11 '08 #4

P: n/a
hu*****@gmail.com wrote:
On Jul 10, 1:28 pm, "mc" <mc_r...@yahoo.comwrote:
>Hello,

I'm trying to create a class (A) with a default constructor friend to
another one (B) as follows:

class A
{
public:
A(int a);
~A();

private:
A(); // default constructor

}

class B
{
// must be able to use A::A()

}

What I need is that only A and B can use the private default constructor
(A::A()). By adding a "friend class B;" in class A, I got it to work but I
do not want B to be able to access the whole of A. Is there a way to do
this? Thank you in advance.

Regards,

MC

you could also try this:
class B; // forward declare B but define it later

class A
{
public:
A(int a);
~A();

private:
A(); // default constructor

friend class B; // say B is a friend

}

class B // now define B
{
// must be able to use A::A()

}
This solution allows 'B' to see _everything_ in 'A', not just its
private default c-tor.

BTW, try removing the forward declaration of 'B'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 11 '08 #5

P: n/a
mc
Victor's correct: B can see everything in A and the forward declaration is
not needed (at least with GCC)
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:g5**********@news.datemas.de...
hu*****@gmail.com wrote:
>On Jul 10, 1:28 pm, "mc" <mc_r...@yahoo.comwrote:
>>Hello,

I'm trying to create a class (A) with a default constructor friend to
another one (B) as follows:

class A
{
public:
A(int a);
~A();

private:
A(); // default constructor

}

class B
{
// must be able to use A::A()

}

What I need is that only A and B can use the private default constructor
(A::A()). By adding a "friend class B;" in class A, I got it to work
but I
do not want B to be able to access the whole of A. Is there a way to do
this? Thank you in advance.

Regards,

MC

you could also try this:
class B; // forward declare B but define it later

class A
{
public:
A(int a);
~A();

private:
A(); // default constructor

friend class B; // say B is a friend

}

class B // now define B
{
// must be able to use A::A()

}

This solution allows 'B' to see _everything_ in 'A', not just its private
default c-tor.

BTW, try removing the forward declaration of 'B'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Jul 11 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.