Solved

member variables

Posted on 2002-05-16
9
321 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 50 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

838 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