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

Protected member friend

P: n/a
MiG
Hello,

Consider the following code snippet:

class B
{
protected:
B() {}
~B() {}
};

class A
{
friend B:B();

public:
A() {}
~A() {}
};

Compiling this code produces the following error:
error C2248: 'B::B' : cannot access protected member declared in class 'B'
Why can I not give B's constructor privileged access to A?
Aug 10 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
MiG wrote:
Hello,

Consider the following code snippet:

class B
{
protected:
B() {}
~B() {}
};

class A
{
friend B:B();
typo : =::
>
public:
A() {}
~A() {}
};

Compiling this code produces the following error:
>error C2248: 'B::B' : cannot access protected member declared in class 'B'
you got the error wrong
It means you can't access B::B within A(when you're granting friend),
because B::B is protected
>
Why can I not give B's constructor privileged access to A?
There are two ways to solve this

way 1:
class A;

class B
{
friend class A;
protected:
B() {}
~B() {}
};

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

//////////////////////////

way 2:

class B
{
public:
B() {}
~B() {}
};

class A
{
friend B::B();
public:
A() {}
~A() {}
};
Aug 10 '07 #2

P: n/a
Barry wrote:
MiG wrote:
>Hello,

Consider the following code snippet:

class B
{
protected:
B() {}
~B() {}
};

class A
{
friend B:B();

typo : =::
>>
public:
A() {}
~A() {}
};

Compiling this code produces the following error:
>>error C2248: 'B::B' : cannot access protected member declared in
class 'B'

you got the error wrong
It means you can't access B::B within A(when you're granting friend),
because B::B is protected
>>
Why can I not give B's constructor privileged access to A?

There are two ways to solve this
The first "way" is an unnecessarily complicated version of the
second "way", which means there is only one way - just grant the
friendship of 'A' to 'B::B()' and that's all.
>
way 1:
class A;
No need.
>
class B
{
friend class A;
No need either. Test it. When friendship is granted, access
is not required. If 'A' grants friendship to a function of 'B',
it does not have to have access to that function of 'B'.
protected:
B() {}
~B() {}
};

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

//////////////////////////

way 2:

class B
{
public:
B() {}
~B() {}
};

class A
{
friend B::B();
public:
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
Aug 10 '07 #3

P: n/a
Victor Bazarov wrote:
Barry wrote:
>MiG wrote:
>>Hello,

Consider the following code snippet:

class B
{
protected:
B() {}
~B() {}
};

class A
{
friend B:B();
typo : =::
>>public:
A() {}
~A() {}
};

Compiling this code produces the following error:

error C2248: 'B::B' : cannot access protected member declared in
class 'B'
you got the error wrong
It means you can't access B::B within A(when you're granting friend),
because B::B is protected
>>Why can I not give B's constructor privileged access to A?
There are two ways to solve this

The first "way" is an unnecessarily complicated version of the
second "way", which means there is only one way - just grant the
friendship of 'A' to 'B::B()' and that's all.
>way 1:
class A;

No need.
>class B
{
friend class A;

No need either. Test it. When friendship is granted, access
is not required. If 'A' grants friendship to a function of 'B',
it does not have to have access to that function of 'B'.
Well, my compiler(msvc8.0) hates the program again

I try msvc6.0 and icl 9.1 both work
Aug 10 '07 #4

P: n/a
Hi!

Barry schrieb:
way 2:

class B
{
public:
B() {}
~B() {}
};

class A
{
friend B::B();
public:
A() {}
~A() {}
};
In this case you wouldn't need "friend" at all, would you?

Frank
Aug 10 '07 #5

P: n/a
Frank Birbacher wrote:
Hi!

Barry schrieb:
>way 2:

class B
{
public:
B() {}
~B() {}
};

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

In this case you wouldn't need "friend" at all, would you?
Of course not. To make it closer to the test condition, do:

class B
{
public:
B();
~B();
};

class A
{
int i;
friend B::B();
public:
A() {}
~A() {}
};

B::B() {
A a;
a.i = 42;
}

B::~B() {
A a;
a.i = 42; // shouldn't compile
}

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

P: n/a
Hi!

Barry schrieb:
Well, my compiler(msvc8.0) hates the program again
I tried msvc8 express on this:

class B
{
friend class A;
protected:
B() {}
~B() {}
};

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

which compiles cleanly.

Frank
Aug 10 '07 #7

P: n/a
Frank Birbacher wrote:
Barry schrieb:
>Well, my compiler(msvc8.0) hates the program again

I tried msvc8 express on this:

class B
{
friend class A;
^^^^^^^^^^^^^^^
Drop this, it's unnecessary.
protected:
B() {}
~B() {}
};

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

which compiles cleanly.
Try actually exercising the friendship next time. See my other
post for a program that does that.

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

P: n/a
MiG
Victor Bazarov wrote:
The first "way" is an unnecessarily complicated version of the
second "way", which means there is only one way - just grant the
friendship of 'A' to 'B::B()' and that's all.
I need to force specialization of a non-abstract class and thought that
the way to do this would be by protecting the constructors.

Is there any other way of doing this without protecting the constructors
and not making the class abstract?

Thanks Victor (and everybody) for your replies.
Aug 10 '07 #9

P: n/a
Victor Bazarov wrote:
Frank Birbacher wrote:
>Barry schrieb:
>>Well, my compiler(msvc8.0) hates the program again
I tried msvc8 express on this:

class B
{
friend class A;
^^^^^^^^^^^^^^^
Drop this, it's unnecessary.
to Frank:

if this is drop msvc8.0 fails to compile, maybe you should view right
from the OP to see what was going on. :-)

///////////////////////////

Thanks V

I think this is a big bug in msvc8.0
Aug 10 '07 #10

P: n/a
Hi!

Victor Bazarov schrieb:
Try actually exercising the friendship next time. See my other
post for a program that does that.
I'm sorry. I missed your point in my last post. Now I got it. I used the
following program:

class B
{
protected:
B() {}
~B() {}
};

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

And I happen not to have any compiler which accepts it. I tried:
MinGW 3.4.2
MSVC 8 Express
linuc gcc 4.1.2

So, even gcc 4.1.2 rejects it o_O

But I tried the EDG front end on the Dinkumware website. And tried
Comeau (same front end, isn't it?). Both accept it. And on the
Dinkumware site I ran MSVC 6 which also accepts the code. So Microsoft
didn't improve on this issue.

Frank
Aug 10 '07 #11

P: n/a
MiG
MiG wrote:
Victor Bazarov wrote:
>The first "way" is an unnecessarily complicated version of the
second "way", which means there is only one way - just grant the
friendship of 'A' to 'B::B()' and that's all.

I need to force specialization of a non-abstract class and thought that
the way to do this would be by protecting the constructors.

Is there any other way of doing this without protecting the constructors
and not making the class abstract?

Thanks Victor (and everybody) for your replies.

Lack of sleep has its consequences, I guess.

Made a crucial mistake in my post. I meant my question to be:

Is there any other way of doing this without *de-protecting* the
constructors and not making the class abstract?
--
- Miguel
Aug 11 '07 #12

P: n/a
Made a crucial mistake in my post. I meant my question to be:

Is there any other way of doing this without *de-protecting* the
constructors and not making the class abstract?

You can write:

class B
{
public :
B(){}

protected:
~B(){}
};

class A
{
friend B::B();

public:
A(){}
~A(){}
};

normal users won't be able to create an object of class B and B::B()
has access to all class A members.

Aug 12 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.