Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

member variables

Posted on 2002-05-16
9
Medium Priority
?
329 Views
Last Modified: 2010-04-02
Is there a way to allow a member variable to be read and written to by its own class but read by all others
0
Comment
Question by:jgreaves
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 7014224
Yes and no - you cannot set members to 'read only', but make them 'private' to your class and offer access methods, e.g.

class C {

public:
 C () {};
 ~C () {};

 int GetValue () const { return value;}

private:

 int value;
};
0
 

Author Comment

by:jgreaves
ID: 7014310
Makes sense

Thanks,

jkr
0
 
LVL 86

Expert Comment

by:jkr
ID: 7014485
Hmm, so could we move this one to the PAQ section or is there anything else you need to know? :o)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 7015304
Another method would be to add a public constant reference to a private member variable.
Example:
class C {
public:
C ()xxvalue(m_xxvalue) {};
const int &xxvalue;
private:
int m_xxvalue;
};

Now any out side class can access xxvalue, but it can't modify it.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7015306
Correction:
class C {
public:
C () : xxvalue(m_xxvalue) {};
const int &xxvalue;
private:
int m_xxvalue;
};
0
 

Expert Comment

by:GuruVB
ID: 7021034
Yes In VB u can do that,

Make a friend Let property and make public Get property. Doing this u can  modify the property (Member variable) which is friend and the others can read this property (member varable)

e.g.

Friend Property Let ErrorMsg(sMsg As String)
    ' Keep the value im mem
    msErrorMsg = sMsg
End Property

Public Property Get ErrorMsg() As String
    ErrorMsg = msErrorMsg
End Property

0
 
LVL 1

Expert Comment

by:llap
ID: 7054960
Another alternative you might find useful is to use
static public members that are initialized by static
functions.
0
 
LVL 11

Expert Comment

by:griessh
ID: 7260193
Dear jgreaves

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "jkr"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7332834
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question