public versus private

Somewhere here, i dont remember where exactly bjarne stroustrup says that the idea of implementing private variables was an idea they had back then, and it was bad.
http://www.youtube.com/watch?v=0iWb_qi2-uI

I dont really understand the purpose of getters and setters either? why not just make the data public?

EDIT:
1:13:26
1:14:08
Last edited on
closed account (Dy7SLyTq)
i didnt watch the video, but its probably because protected is better. and getters/setters provide a cleaner interface imo. they are also helpful with non builtin array containers, such as at() and []
He was saying protected data are a bad idea, not private.

"I think that protected data is a pretty hopeless idea...sounded like a good idea but it turned out not to be. I prefer my data to be public or private."

He mentions it a short moment later after OP's time stamps, and that's all I really watched.
Last edited on
The question he's answering is regarding properties with getters and setters. He says that protected data is "a pretty hopeless idea, it got in, sounded like a good idea, turned out not to be." He goes on to say that he prefers either public or private data. My understanding is that Bjarne holds the position that data should be private if and only-if it represents an invariant. That is, there exists some invalid value for the data that the class should check for.

A setter allows you to check that the value is valid for the data member being altered. void setSomeVal(int x) { if (x > 0) { m_x = x; } }
I dont really understand the purpose of getters and setters either? why not just make the data public?

It's about separating out the interface, which should adopt the semantics of the services your class is providing to the code which uses it, from the implementation details, which should be whatever they need to be to get the job done.

The implementation details may change as you develop the class, but the purpose of the class should remain stable, which means that the interface can remain stable, which means the interactions of the rest of the code with the class can remain stable.

If your interface is just a bunch of getters and setters for each data member, then you've almost certainly not thought properly about the design of your class and what it's actually for.

Really, getters and setters are useful for tutorial purposes. They introduce the idea of hiding the implementation details behind an interface, in a simple-to-understand way. It's a stepping-stone towards learning how to write a good interface.

The problem is that many people teaching this stuff don't then go on to deepen their pupils' understanding of what makes a good interface.
Last edited on
Topic archived. No new replies allowed.