Solved

member variables

Posted on 2002-05-16
9
323 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
Industry Leaders: 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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.

734 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