473,325 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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
Jul 22 '05 #1
28 2820

"Shea Martin" <sm*****@arcis.com> wrote in message
news:Yljdc.4779$G3.43042@localhost...
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
Jul 22 '05 #2

"Shea Martin" <sm*****@arcis.com> wrote in message
news:Yljdc.4779$G3.43042@localhost...
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
Jul 22 '05 #3
On Thu, 8 Apr 2004 22:24:00 +0100, "John Harrison"
<jo*************@hotmail.com> wrote:

"Shea Martin" <sm*****@arcis.com> wrote in message
news:Yljdc.4779$G3.43042@localhost...
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


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


--
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
Jul 22 '05 #4
On Thu, 8 Apr 2004 22:24:00 +0100, "John Harrison"
<jo*************@hotmail.com> wrote:

"Shea Martin" <sm*****@arcis.com> wrote in message
news:Yljdc.4779$G3.43042@localhost...
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


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


--
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
Jul 22 '05 #5
John Harrison wrote:
"Shea Martin" <sm*****@arcis.com> wrote in message
news:Yljdc.4779$G3.43042@localhost...
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

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
Jul 22 '05 #6
John Harrison wrote:
"Shea Martin" <sm*****@arcis.com> wrote in message
news:Yljdc.4779$G3.43042@localhost...
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

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
Jul 22 '05 #7
On Fri, 09 Apr 2004 01:13:15 GMT, Shea Martin <sh**@snowsquirrel.ca> wrote:

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


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
Jul 22 '05 #8
On Fri, 09 Apr 2004 01:13:15 GMT, Shea Martin <sh**@snowsquirrel.ca> wrote:

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


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
Jul 22 '05 #9
Leor Zolman wrote:
On Fri, 09 Apr 2004 01:13:15 GMT, Shea Martin <sh**@snowsquirrel.ca> wrote:

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

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
Jul 22 '05 #10
Leor Zolman wrote:
On Fri, 09 Apr 2004 01:13:15 GMT, Shea Martin <sh**@snowsquirrel.ca> wrote:

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

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
Jul 22 '05 #11

<snip>

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.
Jul 22 '05 #12

<snip>

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.
Jul 22 '05 #13
> 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
Jul 22 '05 #14
> 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
Jul 22 '05 #15
John Harrison wrote:
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.


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.
Jul 22 '05 #16
John Harrison wrote:
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.


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.
Jul 22 '05 #17

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:tM*****************@newsread1.news.pas.earthl ink.net...
John Harrison wrote:
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.


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
Jul 22 '05 #18

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:tM*****************@newsread1.news.pas.earthl ink.net...
John Harrison wrote:
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.


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
Jul 22 '05 #19
John Harrison wrote:
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.

--
Regards,
Buster.
Jul 22 '05 #20
John Harrison wrote:
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.

--
Regards,
Buster.
Jul 22 '05 #21
On Fri, 09 Apr 2004 04:52:16 GMT, Shea Martin <sh**@snowsquirrel.ca> wrote:

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
Thanks,

~S


--
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
Jul 22 '05 #22
On Fri, 09 Apr 2004 04:52:16 GMT, Shea Martin <sh**@snowsquirrel.ca> wrote:

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
Thanks,

~S


--
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
Jul 22 '05 #23
Buster wrote:
John Harrison wrote:
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.


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.
Jul 22 '05 #24
Buster wrote:
John Harrison wrote:
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.


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.
Jul 22 '05 #25
John Harrison wrote:
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


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
Jul 22 '05 #26
John Harrison wrote:
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


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
Jul 22 '05 #27

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c5*************@ID-196037.news.uni-berlin.de...
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


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.
Jul 22 '05 #28

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c5*************@ID-196037.news.uni-berlin.de...
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


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.
Jul 22 '05 #29

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
6
by: blueblueblue2005 | last post by:
here is a friend function of Class Array, which has two private data member: int size, int *ptr // Array's public member function to return size int getSize() const { return size; } friend...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
4
by: Dennis C. Drumm | last post by:
Is there a way with C# to allow one class access to a method or field of another class, without making that method or field visible to all other classes, as would be the case when making the method...
3
by: Chua Wen Ching | last post by:
Hi there, I had seen examples for classes, but i had no idea how to implement the same thing in struct. I am quite mix up! Which one is correct? Scenario: WForm.cs - the one that calls...
10
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base classes.( I already have the ReflectionPermission )...
6
by: Ken Varn | last post by:
I have an ASP.NET application that is calling a custom class that is trying to parse all of the members of my Page object using Type.GetMembers(). The problem that I am having is that private...
6
by: Plamen Doykov | last post by:
Hi all I have converted a simple project from ASP.NET 1 to 2.0 with the latest prerelease of Visual Studio 2005. The problem is I can't access internal members from the code behind. It gives:...
9
by: JT | last post by:
Here is the overall structure I will be referring to: End-program ProvideWorkFlow.dll Forms and methods that properly manipulate calls to methods in AccessUtils AccessUtils (a web service)...
26
by: Zytan | last post by:
What happens if I do this: static byte MemberFunction() instead of: public static byte MemberFunction() I know I can't access it. But what does it default to? Private? I can't find any code...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.