Solved

member variables

Posted on 2002-05-16
9
319 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Handling string inputs in C/Linux 23 168
C++ assignment question 7 132
c++ syntax question 9 36
Header of docx file 17 60
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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now