access to private members of internal class | | |
Class A
{
public:
class B;
int funct(const B &b);
};
Class A::B
{
private:
int _member;
};
int A::funct(const A::B &b)
{
return 10*b._member; //won't compile
}
////////////////////////////
The above code will not compile, as I get a error to the effect of
member _member not accessable from A::funct(const A::B&)
Is there anyway around this, short of declaring _member to be public?
Thanks,
~S | | | | re: access to private members of internal class
"Shea Martin" <smartin@arcis.com> wrote in message
news:Yljdc.4779$G3.43042@localhost...[color=blue]
> Class A
> {
> public:
> class B;
> int funct(const B &b);
> };
>
> Class A::B
> {
> private:
> int _member;
> };
>
> int A::funct(const A::B &b)
> {
> return 10*b._member; //won't compile
> }
>
> ////////////////////////////
> The above code will not compile, as I get a error to the effect of
> member _member not accessable from A::funct(const A::B&)
>
> Is there anyway around this, short of declaring _member to be public?
>
> Thanks,
>
> ~S[/color]
Make A a friend of B.
class A::B
{
friend class A;
...
john | | | | re: access to private members of internal class
"Shea Martin" <smartin@arcis.com> wrote in message
news:Yljdc.4779$G3.43042@localhost...[color=blue]
> Class A
> {
> public:
> class B;
> int funct(const B &b);
> };
>
> Class A::B
> {
> private:
> int _member;
> };
>
> int A::funct(const A::B &b)
> {
> return 10*b._member; //won't compile
> }
>
> ////////////////////////////
> The above code will not compile, as I get a error to the effect of
> member _member not accessable from A::funct(const A::B&)
>
> Is there anyway around this, short of declaring _member to be public?
>
> Thanks,
>
> ~S[/color]
Make A a friend of B.
class A::B
{
friend class A;
...
john | | | | re: access to private members of internal class
On Thu, 8 Apr 2004 22:24:00 +0100, "John Harrison"
<john_andronicus@hotmail.com> wrote:
[color=blue]
>
>"Shea Martin" <smartin@arcis.com> wrote in message
>news:Yljdc.4779$G3.43042@localhost...[color=green]
>> Class A
>> {
>> public:
>> class B;
>> int funct(const B &b);
>> };
>>
>> Class A::B
>> {
>> private:
>> int _member;
>> };
>>
>> int A::funct(const A::B &b)
>> {
>> return 10*b._member; //won't compile
>> }
>>
>> ////////////////////////////
>> The above code will not compile, as I get a error to the effect of
>> member _member not accessable from A::funct(const A::B&)
>>
>> Is there anyway around this, short of declaring _member to be public?
>>
>> Thanks,
>>
>> ~S[/color]
>
>Make A a friend of B.
>
>class A::B
>{
> friend class A;
> ...
>
>john[/color]
Alternatively, /if/ A's functions don't need to /change/ the value of
_member, then having A::B provide a public accessor function would be
another possibility. (But that's a big "if", since this is evidently not
the code Shea actually tried to compile.)
-leor
[color=blue]
>[/color]
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html | | | | re: access to private members of internal class
On Thu, 8 Apr 2004 22:24:00 +0100, "John Harrison"
<john_andronicus@hotmail.com> wrote:
[color=blue]
>
>"Shea Martin" <smartin@arcis.com> wrote in message
>news:Yljdc.4779$G3.43042@localhost...[color=green]
>> Class A
>> {
>> public:
>> class B;
>> int funct(const B &b);
>> };
>>
>> Class A::B
>> {
>> private:
>> int _member;
>> };
>>
>> int A::funct(const A::B &b)
>> {
>> return 10*b._member; //won't compile
>> }
>>
>> ////////////////////////////
>> The above code will not compile, as I get a error to the effect of
>> member _member not accessable from A::funct(const A::B&)
>>
>> Is there anyway around this, short of declaring _member to be public?
>>
>> Thanks,
>>
>> ~S[/color]
>
>Make A a friend of B.
>
>class A::B
>{
> friend class A;
> ...
>
>john[/color]
Alternatively, /if/ A's functions don't need to /change/ the value of
_member, then having A::B provide a public accessor function would be
another possibility. (But that's a big "if", since this is evidently not
the code Shea actually tried to compile.)
-leor
[color=blue]
>[/color]
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html | | | | re: access to private members of internal class
John Harrison wrote:[color=blue]
> "Shea Martin" <smartin@arcis.com> wrote in message
> news:Yljdc.4779$G3.43042@localhost...
>[color=green]
>>Class A
>>{
>>public:
>> class B;
>> int funct(const B &b);
>>};
>>
>>Class A::B
>>{
>>private:
>> int _member;
>>};
>>
>>int A::funct(const A::B &b)
>>{
>> return 10*b._member; //won't compile
>>}
>>
>>////////////////////////////
>>The above code will not compile, as I get a error to the effect of
>>member _member not accessable from A::funct(const A::B&)
>>
>>Is there anyway around this, short of declaring _member to be public?
>>
>>Thanks,
>>
>>~S[/color]
>
>
> Make A a friend of B.
>
> class A::B
> {
> friend class A;
> ...
>
> john
>
>[/color]
I already tried this, and get this error message:
"A.h line 4, Error: Iterator is not a structure tag."
I have tried using Sun's CC 5.0 and 5.6 beta compilers.
~S | | | | re: access to private members of internal class
John Harrison wrote:[color=blue]
> "Shea Martin" <smartin@arcis.com> wrote in message
> news:Yljdc.4779$G3.43042@localhost...
>[color=green]
>>Class A
>>{
>>public:
>> class B;
>> int funct(const B &b);
>>};
>>
>>Class A::B
>>{
>>private:
>> int _member;
>>};
>>
>>int A::funct(const A::B &b)
>>{
>> return 10*b._member; //won't compile
>>}
>>
>>////////////////////////////
>>The above code will not compile, as I get a error to the effect of
>>member _member not accessable from A::funct(const A::B&)
>>
>>Is there anyway around this, short of declaring _member to be public?
>>
>>Thanks,
>>
>>~S[/color]
>
>
> Make A a friend of B.
>
> class A::B
> {
> friend class A;
> ...
>
> john
>
>[/color]
I already tried this, and get this error message:
"A.h line 4, Error: Iterator is not a structure tag."
I have tried using Sun's CC 5.0 and 5.6 beta compilers.
~S | | | | re: access to private members of internal class
On Fri, 09 Apr 2004 01:13:15 GMT, Shea Martin <shea@snowsquirrel.ca> wrote:
[color=blue][color=green]
>>
>> Make A a friend of B.
>>
>> class A::B
>> {
>> friend class A;
>> ...
>>
>> john
>>
>>[/color]
>I already tried this, and get this error message:
>"A.h line 4, Error: Iterator is not a structure tag."[/color]
The solution John gave would work for code "such as" that you've shown.
How can you possibly expect anyone to help you diagnose the cause of that
error message from the information you've supplied? We don't know what
Iterator is, or why the compiler would possibly think it is being used as a
structure tag (um, does it follow the word 'struct'?) But if you showed us
line 4 (or more) of A.h, we might at least /begin/ to be able to take wild
guesses...
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html | | | | re: access to private members of internal class
On Fri, 09 Apr 2004 01:13:15 GMT, Shea Martin <shea@snowsquirrel.ca> wrote:
[color=blue][color=green]
>>
>> Make A a friend of B.
>>
>> class A::B
>> {
>> friend class A;
>> ...
>>
>> john
>>
>>[/color]
>I already tried this, and get this error message:
>"A.h line 4, Error: Iterator is not a structure tag."[/color]
The solution John gave would work for code "such as" that you've shown.
How can you possibly expect anyone to help you diagnose the cause of that
error message from the information you've supplied? We don't know what
Iterator is, or why the compiler would possibly think it is being used as a
structure tag (um, does it follow the word 'struct'?) But if you showed us
line 4 (or more) of A.h, we might at least /begin/ to be able to take wild
guesses...
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html | | | | re: access to private members of internal class
Leor Zolman wrote:[color=blue]
> On Fri, 09 Apr 2004 01:13:15 GMT, Shea Martin <shea@snowsquirrel.ca> wrote:
>
>[color=green][color=darkred]
>>>Make A a friend of B.
>>>
>>>class A::B
>>>{
>>> friend class A;
>>> ...
>>>
>>>john
>>>
>>>[/color]
>>
>>I already tried this, and get this error message:
>>"A.h line 4, Error: Iterator is not a structure tag."[/color]
>
>
> The solution John gave would work for code "such as" that you've shown.
>
> How can you possibly expect anyone to help you diagnose the cause of that
> error message from the information you've supplied? We don't know what
> Iterator is, or why the compiler would possibly think it is being used as a
> structure tag (um, does it follow the word 'struct'?) But if you showed us
> line 4 (or more) of A.h, we might at least /begin/ to be able to take wild
> guesses...
> -leor
>[/color]
Iterator is class B. I forgot to modify what I 'pasted'. I greatly apologize to
Leor for wasting his precious time, and even more precious thought.
I have not used 'friends' very often, and I made the misktake of declaring B to
be a friend of A's. Instead of what John had correctly suggested, Making A a
friend of B.
Thanks,
~S | | | | re: access to private members of internal class
Leor Zolman wrote:[color=blue]
> On Fri, 09 Apr 2004 01:13:15 GMT, Shea Martin <shea@snowsquirrel.ca> wrote:
>
>[color=green][color=darkred]
>>>Make A a friend of B.
>>>
>>>class A::B
>>>{
>>> friend class A;
>>> ...
>>>
>>>john
>>>
>>>[/color]
>>
>>I already tried this, and get this error message:
>>"A.h line 4, Error: Iterator is not a structure tag."[/color]
>
>
> The solution John gave would work for code "such as" that you've shown.
>
> How can you possibly expect anyone to help you diagnose the cause of that
> error message from the information you've supplied? We don't know what
> Iterator is, or why the compiler would possibly think it is being used as a
> structure tag (um, does it follow the word 'struct'?) But if you showed us
> line 4 (or more) of A.h, we might at least /begin/ to be able to take wild
> guesses...
> -leor
>[/color]
Iterator is class B. I forgot to modify what I 'pasted'. I greatly apologize to
Leor for wasting his precious time, and even more precious thought.
I have not used 'friends' very often, and I made the misktake of declaring B to
be a friend of A's. Instead of what John had correctly suggested, Making A a
friend of B.
Thanks,
~S | | | | re: access to private members of internal class
<snip>[color=blue][color=green]
> >
> >[/color]
> I already tried this, and get this error message:
> "A.h line 4, Error: Iterator is not a structure tag."
>
> I have tried using Sun's CC 5.0 and 5.6 beta compilers.
>
> ~S[/color]
The problems i see with your code are
a) "Class" should be replaced by "class" (probably the error you are seeing
by compiler)
b) its unclear what your goal is (just wanting code to be compileable won't
help)
c) member variables of a class should not use the underscore as first
character
d) You should specify constructors with an initializer list to pass
"member_" a value when constructor is invoked (so you can at least test
without stepping the debugger)
e) specify a virtual destructor as well.
f) show minimal code used to invoke the object(s) construction(s) and
behaviours.
You could have friendified class A as mentioned in other posts but i'ld
prefer doing the same to the function instead (friend A::func(...)). But if
class A is composed of a class B, that just doesn't make sense. add a public
function to class B which has access to it's own private member instead. | | | | re: access to private members of internal class
<snip>[color=blue][color=green]
> >
> >[/color]
> I already tried this, and get this error message:
> "A.h line 4, Error: Iterator is not a structure tag."
>
> I have tried using Sun's CC 5.0 and 5.6 beta compilers.
>
> ~S[/color]
The problems i see with your code are
a) "Class" should be replaced by "class" (probably the error you are seeing
by compiler)
b) its unclear what your goal is (just wanting code to be compileable won't
help)
c) member variables of a class should not use the underscore as first
character
d) You should specify constructors with an initializer list to pass
"member_" a value when constructor is invoked (so you can at least test
without stepping the debugger)
e) specify a virtual destructor as well.
f) show minimal code used to invoke the object(s) construction(s) and
behaviours.
You could have friendified class A as mentioned in other posts but i'ld
prefer doing the same to the function instead (friend A::func(...)). But if
class A is composed of a class B, that just doesn't make sense. add a public
function to class B which has access to it's own private member instead. | | | | re: access to private members of internal class
> c) member variables of a class should not use the underscore as first[color=blue]
> character[/color]
Why? I do this all the time. I get moaned at on style grounds but I yet to
hear of concrete reason why its bad.
john | | | | re: access to private members of internal class
> c) member variables of a class should not use the underscore as first[color=blue]
> character[/color]
Why? I do this all the time. I get moaned at on style grounds but I yet to
hear of concrete reason why its bad.
john | | | | re: access to private members of internal class
John Harrison wrote:[color=blue][color=green]
>>c) member variables of a class should not use the underscore as first
>>character[/color]
>
>
> Why? I do this all the time. I get moaned at on style grounds but I yet to
> hear of concrete reason why its bad.
>[/color]
It's not bad. But getting in the habit of using identifiers that begin
with an underscore is.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. | | | | re: access to private members of internal class
John Harrison wrote:[color=blue][color=green]
>>c) member variables of a class should not use the underscore as first
>>character[/color]
>
>
> Why? I do this all the time. I get moaned at on style grounds but I yet to
> hear of concrete reason why its bad.
>[/color]
It's not bad. But getting in the habit of using identifiers that begin
with an underscore is.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. | | | | re: access to private members of internal class
"Kevin Goodsell" <usenet2.spamfree.fusion@neverbox.com> wrote in message
news:tMrdc.2885$A_4.1069@newsread1.news.pas.earthl ink.net...[color=blue]
> John Harrison wrote:[color=green][color=darkred]
> >>c) member variables of a class should not use the underscore as first
> >>character[/color]
> >
> >
> > Why? I do this all the time. I get moaned at on style grounds but I yet[/color][/color]
to[color=blue][color=green]
> > hear of concrete reason why its bad.
> >[/color]
>
> It's not bad. But getting in the habit of using identifiers that begin
> with an underscore is.
>[/color]
By identifiers to you mean identifiers generally? So that member variables
are ok, but other identifiers might not be. Since I use a leading underscore
precisely to indicate a member variable that means I'm ok?
john | | | | re: access to private members of internal class
"Kevin Goodsell" <usenet2.spamfree.fusion@neverbox.com> wrote in message
news:tMrdc.2885$A_4.1069@newsread1.news.pas.earthl ink.net...[color=blue]
> John Harrison wrote:[color=green][color=darkred]
> >>c) member variables of a class should not use the underscore as first
> >>character[/color]
> >
> >
> > Why? I do this all the time. I get moaned at on style grounds but I yet[/color][/color]
to[color=blue][color=green]
> > hear of concrete reason why its bad.
> >[/color]
>
> It's not bad. But getting in the habit of using identifiers that begin
> with an underscore is.
>[/color]
By identifiers to you mean identifiers generally? So that member variables
are ok, but other identifiers might not be. Since I use a leading underscore
precisely to indicate a member variable that means I'm ok?
john | | | | re: access to private members of internal class
John Harrison wrote:
[color=blue]
> By identifiers to you mean identifiers generally? So that member variables
> are ok, but other identifiers might not be. Since I use a leading underscore
> precisely to indicate a member variable that means I'm ok?[/color]
Provided the second character of the member identifier is a lower case
letter, yes, you're OK.
--
Regards,
Buster. | | | | re: access to private members of internal class
John Harrison wrote:
[color=blue]
> By identifiers to you mean identifiers generally? So that member variables
> are ok, but other identifiers might not be. Since I use a leading underscore
> precisely to indicate a member variable that means I'm ok?[/color]
Provided the second character of the member identifier is a lower case
letter, yes, you're OK.
--
Regards,
Buster. | | | | re: access to private members of internal class
On Fri, 09 Apr 2004 04:52:16 GMT, Shea Martin <shea@snowsquirrel.ca> wrote:
[color=blue][color=green]
>>[/color]
>Iterator is class B. I forgot to modify what I 'pasted'. I greatly apologize to
>Leor for wasting his precious time, and even more precious thought.[/color]
This makes me sound like I'm being some kind of a noodnik (sorry, that's a
Polish term I grew up and I don't know any English terms that do justice to
it; but it is sort of like "primadonna" combined with Dennis the Menace). I
suspect it wasn't just /my/ time that you "wasted", but by posting I was
hoping to a) bring the facts I outlined to your attention, and b) save a
bit of time for the folks who scan ahead in a thread to see if it has been
resolved or not before putting more effort into responding to the original
(or intermediate) question(s).
[color=blue]
>
>I have not used 'friends' very often, and I made the misktake of declaring B to
>be a friend of A's. Instead of what John had correctly suggested, Making A a
>friend of B.[/color]
Glad the group was able to help,
-leor[color=blue]
>
>Thanks,
>
>~S[/color]
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html | | | | re: access to private members of internal class
On Fri, 09 Apr 2004 04:52:16 GMT, Shea Martin <shea@snowsquirrel.ca> wrote:
[color=blue][color=green]
>>[/color]
>Iterator is class B. I forgot to modify what I 'pasted'. I greatly apologize to
>Leor for wasting his precious time, and even more precious thought.[/color]
This makes me sound like I'm being some kind of a noodnik (sorry, that's a
Polish term I grew up and I don't know any English terms that do justice to
it; but it is sort of like "primadonna" combined with Dennis the Menace). I
suspect it wasn't just /my/ time that you "wasted", but by posting I was
hoping to a) bring the facts I outlined to your attention, and b) save a
bit of time for the folks who scan ahead in a thread to see if it has been
resolved or not before putting more effort into responding to the original
(or intermediate) question(s).
[color=blue]
>
>I have not used 'friends' very often, and I made the misktake of declaring B to
>be a friend of A's. Instead of what John had correctly suggested, Making A a
>friend of B.[/color]
Glad the group was able to help,
-leor[color=blue]
>
>Thanks,
>
>~S[/color]
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html | | | | re: access to private members of internal class
Buster wrote:
[color=blue]
> John Harrison wrote:
>[color=green]
>> By identifiers to you mean identifiers generally? So that member
>> variables
>> are ok, but other identifiers might not be. Since I use a leading
>> underscore
>> precisely to indicate a member variable that means I'm ok?[/color]
>
>
> Provided the second character of the member identifier is a lower case
> letter, yes, you're OK.
>[/color]
Or even a digit.
And yes, I did mean identifiers in general. For example, you can't
(without Undefined Behavior) use an identifier beginning with an
underscore followed by an uppercase letter, or an identifier containing
a sequence of two underscores in *any* context at all. Presumably this
is to allow the implementation to use identifiers of this form for
unscoped identifiers such as macros.
As far as I know, you can use identifiers beginning with an underscore,
followed by a lowercase letter in contexts like:
* Inside your own namespaces (not std, but you're generally not allowed
to add things to std anyway).
* Function local variables, and formal parameters.
* Member variables and functions.
You can also use the identifier '_' and identifiers beginning with an
underscore followed by a digit in any context, I believe.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. | | | | re: access to private members of internal class
John Harrison wrote:[color=blue][color=green]
>>c) member variables of a class should not use the underscore as first
>>character[/color]
>
>
> Why? I do this all the time. I get moaned at on style grounds but I yet to
> hear of concrete reason why its bad.
>
> john
>
>[/color]
What alternatives are used? I often see m_Member, but that is two chars to
prepend to every member, while _member only requires one, plus one less shift.
I have contemplated switching to mMember, but have never got around to it.
One thing I don't do is denote a pointer member in any special way. I guess if
I switched to mMember or m_Member I could do pMember or p_Member.
So many styles, so little time... To me, the biggest thing when I read someone
elses code, is consistency. I dont' really care what their method is, so long
as it is somewhat obvious, and consistent.
~S | | | | re: access to private members of internal class
Buster wrote:
[color=blue]
> John Harrison wrote:
>[color=green]
>> By identifiers to you mean identifiers generally? So that member
>> variables
>> are ok, but other identifiers might not be. Since I use a leading
>> underscore
>> precisely to indicate a member variable that means I'm ok?[/color]
>
>
> Provided the second character of the member identifier is a lower case
> letter, yes, you're OK.
>[/color]
Or even a digit.
And yes, I did mean identifiers in general. For example, you can't
(without Undefined Behavior) use an identifier beginning with an
underscore followed by an uppercase letter, or an identifier containing
a sequence of two underscores in *any* context at all. Presumably this
is to allow the implementation to use identifiers of this form for
unscoped identifiers such as macros.
As far as I know, you can use identifiers beginning with an underscore,
followed by a lowercase letter in contexts like:
* Inside your own namespaces (not std, but you're generally not allowed
to add things to std anyway).
* Function local variables, and formal parameters.
* Member variables and functions.
You can also use the identifier '_' and identifiers beginning with an
underscore followed by a digit in any context, I believe.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. | | | | re: access to private members of internal class
John Harrison wrote:[color=blue][color=green]
>>c) member variables of a class should not use the underscore as first
>>character[/color]
>
>
> Why? I do this all the time. I get moaned at on style grounds but I yet to
> hear of concrete reason why its bad.
>
> john
>
>[/color]
What alternatives are used? I often see m_Member, but that is two chars to
prepend to every member, while _member only requires one, plus one less shift.
I have contemplated switching to mMember, but have never got around to it.
One thing I don't do is denote a pointer member in any special way. I guess if
I switched to mMember or m_Member I could do pMember or p_Member.
So many styles, so little time... To me, the biggest thing when I read someone
elses code, is consistency. I dont' really care what their method is, so long
as it is somewhat obvious, and consistent.
~S | | | | re: access to private members of internal class
"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:c55cbi$2og5sb$1@ID-196037.news.uni-berlin.de...[color=blue][color=green]
> > c) member variables of a class should not use the underscore as first
> > character[/color]
>
> Why? I do this all the time. I get moaned at on style grounds but I yet to
> hear of concrete reason why its bad.
>
> john
>[/color]
I say if you use the underscore followed by a lowercase character within a
namespace for an identifier, don't change your style. Just be aware of the
consequences of using a leading double-underscore or _Member for an
identifier.
Thanks for the feedback, folks. | | | | re: access to private members of internal class
"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:c55cbi$2og5sb$1@ID-196037.news.uni-berlin.de...[color=blue][color=green]
> > c) member variables of a class should not use the underscore as first
> > character[/color]
>
> Why? I do this all the time. I get moaned at on style grounds but I yet to
> hear of concrete reason why its bad.
>
> john
>[/color]
I say if you use the underscore followed by a lowercase character within a
namespace for an identifier, don't change your style. Just be aware of the
consequences of using a leading double-underscore or _Member for an
identifier.
Thanks for the feedback, folks. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|