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? 12 2788
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() {}
};
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
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
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
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
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
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
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.
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chris Mantoulidis |
last post by:
Forgive me if I'm wrong but I think there is something like an extra
member scope in classes.
for example:
class abc {
ostream & operator << (ostream &, const abc &);
istream & operator >>...
|
by: Steven T. Hatton |
last post by:
I find the surprising. If I derive Rectangle from Point, I can access the
members of Point inherited by Rectangle _IF_ they are actually members of a
Rectangle. If I have a member of type Point...
|
by: Adam H. Peterson |
last post by:
I just made an observation and I wondered if it's generally known (or
if I'm missing something). My observation is that static protected
members are essentially useless, only a hint to the user. ...
|
by: yccheok |
last post by:
hello, in a singleton design, i trying to make my parent class having
protected constructor and destructor. however, the compiler give me
error when my child, trying to delete itself through parent...
|
by: |
last post by:
hello there,
what is the difference between Shared and Protected
Shared? where can I read more about theses kind of
variables (or whatever they are....sorry, don't know the
word in eng.)
...
|
by: mlimber |
last post by:
In an article on the safe bool idiom
(http://www.artima.com/cppsource/safeboolP.html), Bjorn Karlsson gives
the following code (slightly modified):
class safe_bool_base {
protected:
typedef...
|
by: Rick |
last post by:
Hi,
Can anyone explain to me why the below fails to compile - seeing
otherA->f(); as a call to a inaccessible function, while otherB->f();
is ok?
It seems you can happily access protected...
|
by: Eric Lilja |
last post by:
Hello, consider this program:
class Outer
{
public:
class Base
{
friend class Outer;
public:
Base(int n) : n_(n) {}
protected:
|
by: Mayur H Chauhan |
last post by:
All,
For my knowledge, if I declare Class as follow, then it thows
compilation error.
Protected Class Book
End Class
Even same for...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |