Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class variables: public access read-only, but private access read/write

Whoopee, not working on that socket library for the moment. I'm trying to educate myself a little more in C++.

With classes, is there a way to make a variable read-only to the public, but read+write when accessed privately? e.g. something like this:

class myClass {     private:     int x; // this could be any type, hypothetically      public:     void f() {         x = 10; // this is OK     } }  int main() {     myClass temp;      // I want this, but with private: it's not allowed     cout << temp.x << endl;       // this is what I want:      // this to be allowed     temp.f(); // this sets x...      // this to be allowed     int myint = temp.x;      // this NOT to be allowed     temp.x = myint; } 

My question, condensed, is how to allow full access to x from within f() but read-only access from anywhere else, i.e. int newint = temp.x; allowed, but temp.x = 5; not allowed? like a const variable, but writable from f()...

EDIT: I forgot to mention that I plan to be returning a large vector instance, using a getX() function would only make a copy of that and it isn't really optimal. I could return a pointer to it, but that's bad practice iirc.

P.S.: Where would I post if I just want to basically show my knowledge of pointers and ask if it's complete or not? Thanks!

like image 523
FurryHead Avatar asked Mar 24 '11 18:03

FurryHead


People also ask

Can you access private variables within a class?

We can access a private variable in a different class by putting that variable with in a Public method and calling that method from another class by creating object of that class.

Can class variables be public?

Public variables, are variables that are visible to all classes. Private variables, are variables that are visible only to the class to which they belong. Protected variables, are variables that are visible only to the class to which they belong, and any subclasses.

Can private variables be read?

Others can read the values of private variables in your contract. It's not as easy and straightforward as it is to read the values of public variables, but it is still feasible. So don't count on the value of a private variable as being secret.

Can you access a public variable outside of it's class?

If you want to use that variable even outside the class, you must declared that variable as a global. Then the variable can be accessed using its name inside and outside the class and not using the instance of the class.


1 Answers

Of course you can:

class MyClass {     int x_;  public:     int x() const { return x_; } }; 

If you don't want to make a copy (for integers, there is no overhead), do the following:

class MyClass {     std::vector<double> v_;  public:     decltype(v)& v() const { return v_; } }; 

or with C++98:

class MyClass {     std::vector<double> v_;  public:     const std::vector<double>& v() const { return v_; } }; 

This does not make any copy. It returns a reference to const.

like image 117
Alexandre C. Avatar answered Nov 09 '22 04:11

Alexandre C.