0

If I have a class:

class A {
public:
    A();
   ~A();

    int something();
}

class B {
public:
    B() {a = new A();};
   ~B();
private:
     A *a;
}

main() {
   B x;
}

Is the only way to use object "a" in main is to have a getter for it?

class B {
public: 
    A getA() {return *a;};
private:
    A *a;
}

And then if I wanted to set the private variable object, I would need a setter, and set it in main?

Edit: I updated class A, B, main a little. Main question is, what is the best way if I create an object of B in main. Can I access the function "something()" in anyway so information can be stored in private variable "a"? I know I can't do x.a.something() because "a" is private.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
JEB
  • 21
  • 1
  • 3
    Maybe you need a C++ book instead of a FAQ? – Mooing Duck Dec 19 '14 at 00:16
  • possible duplicate of [Why use getters and setters?](http://stackoverflow.com/questions/1568091/why-use-getters-and-setters) – Pradhan Dec 19 '14 at 00:17
  • Alternatively, make the variable public. Also note: the `inline` specification is redundant, you're making needless copies of your object, getters usually have a mutable and a `const` version, you should be using smart pointers, you're violating the rule of three... – Mooing Duck Dec 19 '14 at 00:17
  • What are you trying to do? And why did you make `a` a pointer? Note that this will cause you troubles if you don't provide your own copy constructor and assignment operator and destructor. Also note that `getA` returns a copy of `*a`. – 5gon12eder Dec 19 '14 at 00:17
  • I originally had just general code for a question. I have edited the OP a little to maybe better explain. – JEB Dec 19 '14 at 00:29
  • 1
    Are you a Java programmer who is learning C++, JEB? It is difficult to guess (even) from your (updated) code what you are trying to achieve with your classes. Note however that you'll have a severe problem with resource leaks. Object lifetime and copy-semantics are major differences between Java and C++ that call for different design approaches. – 5gon12eder Dec 19 '14 at 00:35
  • Why the downvote? That's a valid question. – πάντα ῥεῖ Dec 19 '14 at 00:49
  • @MooingDuck Let's start from the start. The primary points are well answerable. – πάντα ῥεῖ Dec 19 '14 at 01:21

1 Answers1

2

"Is the only way to use object "a" in main is to have a getter for it?"

Yes.

"And then if I wanted to set the private variable object, I would need a setter, and set it in main?"

Again the answer is: Yes.

The purpose of making a class member variable private is to encapsulate the data into the class, and making access only viable by using function members of this class. The simplest kind of function members are the getter and setter member functions.
They seem to be pretty useless for a naive point of view (at least as you could simply use a public class member variable instead). But encapsulation means you don't need to change anything, if it turns out later, you'll e.g. need to survey setting new values for this member, might need to trigger recalculation of, or any action on other class member variables.


Your class declaration should look like follows (you don't need to use an A* pointer usually)

class A {
};

class B {
public:
    B() : a_() {}
    const A& a() const { return a_; } // Getter
    void a(const A& newValue) { a_ = newValue; } // Setter
private:
     A a_;
};

And use it

int main() {
    A a;
    B x;

    x.a(a); // Set the value
    a = x.a(); // Get the value
}

To extend on your edit:

"Can I access the function something() in anyway so information can be stored in private variable a? I know I can't do x.a.something() because a is private."

You can't access that function directly from the classical getter function (as I had proposed above) like

x.a().something();

because something() isn't a const member function of class A.

To allow manipulation you'll either need to declare the getter returning a non const reference (which actually breaks the mentioned encapsulation rules, and is discouraged because of this):

    A& a() { return a_; } // Getter

or provide a member function in class B, that delegates that call properly:

class B {
public:
    // like all of my proposal from above ...

    int doSomething() {
       return a_.something();
    }
private:
     A a_;
};
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Can the downvoter explain what's actually wrong with this answer please? – πάντα ῥεῖ Dec 19 '14 at 00:24
  • 1
    @Deduplicator Explained a bit more, why encapsulation makes sense from the start. There's no real disadvantage I can see, vs using a public member variable (these rarely pass code reviews of mine besides presenting real POD's, but then I expect to see `struct` instead of `class`). – πάντα ῥεῖ Dec 19 '14 at 00:36