Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Densun

my opinion on protected and private members in c++

This topic is 6933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my opinion, private and protected members in C++ classes are pointless. There is no reason why they can''t be public. I don''t care if the variables can be accidently changed (or methods called). It''s just a bug that needs fixing if that happens. Programming doesn''t need to be user friendly like Microsoft programs are. I''m curious as to what the rest of you think. Don''t worry about hurting my feelings by saying I''m wrong .

Share this post


Link to post
Share on other sites
Advertisement
Okay, you're wrong.

Seriously, in a sense you are correct. There are several features in C++ (and C, and other languages) that (in general) do not affect the compiled code in the slightest. The compiled code you get when field 'xyz' is private is no different from the compiled code if 'xyz' is public. The const keyword is another example.

(Note to compiler junkies: Yes, I know that using this CAN actually change compiled code, usually in the sense that certain things can be more aggressively optimized. But it's not really important to this discussion.)

However, the main benefit of using things like private, protected, public, const, and other is to help programmers write better code. They are a means to an end, not the end itself.

For example, by making certain class members private, I can ensure that I don't screw up their contents accidentally. It doesn't absolutely prevent me from changing them if I really want to.

You say that you don't care if variables can be changed accidentally, and that programming doesn't need to be user-friendly. Can you expand on why you think this?

I've been programming professionally for seven years, and a bit longer than that (back on the ol' C-64). While I won't argue that there are people who can write thousands of lines of spaghetti code and understand every bit of it fluently, I suspect most of us need a helping hand. Especially when it comes to large, complex projects. More especially when you have to work with other people. That's when those sorts of "pointless" compiler keywords come in real handy.

(Although I will admit to sometimes circumventing those mechanisms and making some things public. In the end, I usually wish I hadn't -- for my projects, it makes short-term fixes easy, but can mess up my long-term process.)

---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!


Edited by - mossmoss on 5/1/00 5:39:26 PM

Share this post


Link to post
Share on other sites
They are not pointless. Besides the safety they provide of not changing them accidently, the biggest benefit they provide is that by hiding the data behind "private" they allow you to re-implement the internals of the class without breaking any code that uses the class.

Also, if something is pointless because you don''t need it when writing bug-free code, let''s do away with type-checking, function prototyping, scoping, etc. Heck, let''s go back to hex-based machine code.

aig

Share this post


Link to post
Share on other sites
Sure, we don''t need protected or private members. Nor do we need any kind of object representation, or function overloading, or typechecking. Hell, why not just do everything in assembly?

Protected and private members are tools. They -help- you write code with fewer bugs. Sure, you can make things public, and fix the bugs, but I''d rather teach myself a way of programming that reduces the chances of these bugs happening. And one that allows the compiler to find bugs for me before the program even runs.

Encapsulation, whether done via private/protected members or through some more basic scheme, also helps you change your program more quickly: providing access through an interface rather than through direct access to variables means you can change the 2 halves of your program as much as you like providing the interface stays valid.

I''m sure a good textbook could give you 101 more good reasons for using such tools the language puts at your disposal

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!