Domanda

So I don't know if you any of you have been/goes on the NewBoston.Com (great resource by the way), but I was watching this video and the teacher was saying how you can access private info using public functions...

This is the video: http://www.youtube.com/watch?v=jTS7JTud1qQ

And of course, just skip to the end to get to the gist of it.

The thing I don't get though, is how can you access private information using a public function. If you make a function that's public that allows anyone to set the name, and get that name, then isn't it just public anyway?, Wouldn't anyone be able to mess my code and my desired output?

È stato utile?

Soluzione

The point of providing a public method to change a private variable is that you can add additional controls.

There is not a lot of difference between

class A {
   public:
      int age;
}

and

class B {
   private:
      int age;
   public:
      void setAge(int _age);
}

B::setAge(int _age) {
   this->age = _age;
}

But, in the second case, you can add logic that rejects some data (v.g. a negative value) or updates other fields. So you can ensure that the data of your object will remain consistent. If you follow the first approach, that logic should replicated every time the property is accessed directly (note: many programmers will forget to do so).

Altri suggerimenti

Creating trivial getters and setters (public functions) is a bit futile in my opinion. If your class functionality is fully expressed by the private variables, then trivial getters and setters are a waste and somewhat of a code smell. Maybe you should use a struct instead.

However public functions can provide an interface that guides the usage of the class and restricts what you can do with the private members. You do not want users of your class to change individual private members that only make sense when changed in sync step.

It also lets the implementation change, without changing the interface. For example say you want to increase the size of a hash map. Pseudo code:

class IntHashMap {
public:
void increaseSize(int size);
// Other stuff.

private:
int* backing_array_ ;
int size_;
}

Now your implementation for increaseSize() can actually check if the new size is greater than the previous one. It can also actually create a new backing_array and copy the data over. Exposing the private variable size would achieve neither the check for proper value of size, nor the allocation of the new backing array.

Sorry, I'm not watching the video, but I assume you're talking about things like getters (functions to get the value of a property -- a private data member in this case) and setters (functions to set those values).

If you provide getters and setters as pure pass-through functions then you're correct, it's really not much different than just making the members public. The normal use though is to provide intelligence into your accessor functions -- letting you, for example, place limits on the values that can be set or letting retrieved values be computed rather than only based on a pure value.

If you weren't able to access private data from public functions, you would be able to access it only from private functions, right. However, calling private functions from public functions should consequently also be forbidden, so...

How are you supposed to access private data at all?

The purpose with public "getters/setters" if you will, are the fact that they constrain the user from doing more than you want them to. Instead of them getting/setting the private member (in which they can do anything to it), you can limit what they can/can't do by wrapping the private member in a public accessor. That way, inside this public property/function, you can limit the value they can set it too.

You can also just have the public property/function get the private member, but prevent the user from setting it.

Code with getters/setters are considered 'better' than direct private member access because those functions can then control all of the input/output from those variables and make sure it is of the type, range, or whatever that the class expects. It's a way for the class creator to error-check the data being set or manipulate it before returning it.

If you could just do class.var = value; you could muck things up by putting in the wrong type of variable, or overflowing some limit, or even putting in a legitimate value that some logic in the class's code breaks with. For simple things with a single developer it's usually not a huge issue, but for actual, professional code it's very good practice to encapsulate member variables within a get/set pair for access/writing.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top