4chan archive /g/ (index)
2012-11-12 01:46 29003803 Anonymous (1352611861502.jpg 460x460 68kB)
Programming question here about CLASSES and STRUCTURES. Class: Essentially a structure, but with private variables. What is the point of making them private? It's not like something is going to try and edit the variables anyways. Is it to prevent the programmer from accidentally editing it?

1 min later 29003839 Anonymous
Because if you don't make them private shit gets fucked up. Also classes can hold methods, while structs can't

2 min later 29003846 Anonymous
Sometimes you need variables within your class that you know are only going to be altered internally. This is especially important for variables that maintain state etc. Also, say you want to do some pre/post processing on a value before it truly gets set to the private variable. If you expose getters/setters, you can enforce the pre/post processing logic. These are just a couple of the possible reasons.

2 min later 29003858 Anonymous
>>29003839 structs can hold function pointers.

3 min later 29003869 Anonymous
class MySingleton { private MySingleton instance = NULL; private constructor() { } public staticclassmethodwhatever getInstance() { if (instance == NULL) instance = new MySingleton(); return instance; } }

4 min later 29003880 Anonymous
>>29003858 Virtual functions != function pointers.

16 min later 29004133 Anonymous (lightbulb1.jpg 400x400 19kB)
>>29003803 Objects are supposed to provide a level of abstraction. When you can just change internal fields the abstraction layer doesn't work as well as it could.

1 hours later 29005017 Anonymous
there is a reason you make shit private and either publicly immutable or only mutable through specific channels (mutator functions) OP: http://en.wikipedia.org/wiki/Action_at_a_distance_(computer_programming)

1 hours later 29005161 Anonymous
>make a class with public variables >other classes interact with those variables >want to completely change implementation of first class >cant because too much code depends on those public variables

1.725 0.027