class access to variables

C

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

Victor Bazarov

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

shadowman615

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

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top