access to private members of internal class

S

Shea Martin

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
 
J

John Harrison

Shea Martin said:
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

Make A a friend of B.

class A::B
{
friend class A;
...

john
 
L

Leor Zolman

Make A a friend of B.

class A::B
{
friend class A;
...

john

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
 
S

Shea Martin

John said:
Make A a friend of B.

class A::B
{
friend class A;
...

john
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
 
L

Leor Zolman

I already tried this, and get this error message:
"A.h line 4, Error: Iterator is not a structure tag."

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
 
S

Shea Martin

Leor said:
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
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
 
S

SaltPeter

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

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.
 
J

John Harrison

c) member variables of a class should not use the underscore as first
character

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
 
K

Kevin Goodsell

John said:
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.

It's not bad. But getting in the habit of using identifiers that begin
with an underscore is.

-Kevin
 
J

John Harrison

Kevin Goodsell said:
It's not bad. But getting in the habit of using identifiers that begin
with an underscore is.

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
 
B

Buster

John said:
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?

Provided the second character of the member identifier is a lower case
letter, yes, you're OK.
 
L

Leor Zolman

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.

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).
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.

Glad the group was able to help,
-leor
 
K

Kevin Goodsell

Buster said:
Provided the second character of the member identifier is a lower case
letter, yes, you're OK.

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
 
S

Shea Martin

John said:
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

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
 
S

SaltPeter

John Harrison said:
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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top