Accessing member variables which are classes

Aug 10, 2014 at 5:03pm
Hello, world.

I have a question about accessing member variables which are C++ structs that contain other C++ structs.

In order to keep encapsulation, I need to write getter and setters instead of returning the member variable as a reference, this was fine at first until I started making member variables structs/classes. Consider the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct A {
    int doo;
    char bar;
};

struct B {
    A blam;
    char whop;
};

class C {
private:
    B banana;

public:
    // Do I need to create getter/setters for all the structs?
    // Or should I instead make the structs into classes with the 
    // members private and implement getter/setters in those instead?
};


Any help is appreciated

EDIT:

If I do make the structs classes instead, I think I should still return them by reference so their functions can be called.
Last edited on Aug 10, 2014 at 5:11pm
Aug 10, 2014 at 5:12pm
In order to keep encapsulation, I need to write getter and setters instead of returning the member variable as a reference,


This is the wrong mentality. Very wrong.

Getters/setters are terrible. They are bad for encapsulation. Granted... they're not as bad as public members... but they're still bad.

Classes function better when they are a singular entity... and not just a collection of a bunch of different parts. Getters/setters destroy that singular entity idea and instead pull out a small part of the class -- which breaks encapsulation.

Ideally... if a class is properly encapsulated... outside code shouldn't even know or care what pieces belong to your class.




As with any design question, it is difficult to give a straight answer, since the best way to design something depends a lot on what you're trying to do. So I can't really say what would be the best thing for you in this specific instance.

What I will say, though... is that there is extremely little (if any) value in making variables private if you are just going to write a bunch of getters/setters for all of them. That's nothing but a waste of time.
Aug 10, 2014 at 5:22pm
I see what you are saying.
My structs have no other purpose than storing data in a group (hopefully this is ok), perhaps I should just make them public.
Aug 10, 2014 at 5:25pm
Yes they they are just a clump of data... then a struct with public members is fine. Don't bother trying to encapsulate that.
Last edited on Aug 10, 2014 at 5:25pm
Aug 10, 2014 at 5:26pm
Thanks, BTW this is what made me (I probably incorrectly interpreted it) think getters and setters were important for encapsulation: http://stackoverflow.com/questions/8005514/is-returning-references-of-member-variables-bad-pratice
Aug 10, 2014 at 7:08pm
That thread explains why it is bad to return a reference to a member (which is indeed worse than getters/setters -- and in fact is arguably worse than making members public), but I don't see anywhere in the response where he says using getters/setters is a good idea.
Aug 10, 2014 at 7:21pm
I guess that is where I misinterpreted, it said that if the variable is in turn calculated on the fly, you wouldn't be able to return a reference, however a getter could still return the calculated variable
Topic archived. No new replies allowed.