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

class access to variables

P: n/a
cdg
Is there any way to directly access variables of one class from another
class without passing any public member variables. Similar to how any
function in a class can directly access (without passing an argument) any
member variable of its' own class.
I thought the only way this could be possible would be to make a function
a "friend" of the class. But I now beleive that it is still necessary to
pass variables to the other class, is this true.
Jul 6 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
cdg wrote:
Is there any way to directly access variables of one class from
another class without passing any public member variables. Similar to
how any function in a class can directly access (without passing an
argument) any member variable of its' own class.
Sure. Make them public or declare the class that needs the access
a friend of the other class.
I thought the only way this could be possible would be to make a
function a "friend" of the class. But I now beleive that it is still
necessary to pass variables to the other class, is this true.
No, it is not true. All you need is to declare something a friend.

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

P: n/a

cdg wrote:
Is there any way to directly access variables of one class from another
class without passing any public member variables. Similar to how any
function in a class can directly access (without passing an argument) any
member variable of its' own class.
I thought the only way this could be possible would be to make a function
a "friend" of the class. But I now beleive that it is still necessary to
pass variables to the other class, is this true.
Do you mean private variables? Your explanation is a bit vague.

A few options:

1. Make the variables public.

2. Use public accessor methods that return or modify the values of
private class attributes

3. Make the other *class* a friend of the class that contains the
private variables you want to access.

Jul 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.