Solved

member variables

Posted on 2002-05-16
9
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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

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!

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

735 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