-3

Is there a way to set a private member variable of a base class to a value in the constructor of a derived class?

I understand that's what getter and setter methods are for and what making the variable protected or public is for, but assuming you can't modify the base class, is there any alternate way to set it?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    Why do you want this? – GManNickG May 03 '16 at 02:37
  • If you have no control over the base class its a really bad idea. You are setting yourself up for broken code at an unpredictable time in the future, even if you are not creating undefined behavior right now - which you basically are. – Galik May 03 '16 at 02:40
  • _"but assuming you can't modify the base class, is there any alternate way to set it?"_ Usually not, but does your base class provide a constructor that initializes the private member variables? – πάντα ῥεῖ May 03 '16 at 02:41
  • I'm taking an online test and it strictly says that my base class cannot be modified. The task it's asking is to make a constructor for a derived class that takes in a parameter that changes a private variable in the base class. – Robert Mizuhara May 03 '16 at 02:42
  • Yes it does have a constructor that does. How would this help me? – Robert Mizuhara May 03 '16 at 02:43
  • @RobertMizuhara Again: Does the base class provide a constructor that initializes the private member variables? – πάντα ῥεῖ May 03 '16 at 02:43
  • @ πάντα ῥεῖ Yes it does – Robert Mizuhara May 03 '16 at 02:44
  • 1
    @RobertMizuhara _"How would this help me?"_ You can call it from the [member initializer list](http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor) of the derived constructor. – πάντα ῥεῖ May 03 '16 at 02:46

3 Answers3

6

No. It's private. That is the whole point of private.

From the clarifications in the comments - the base class does give you a way to do it via its constructor, so you can use that.

// Assuming MyBaseclass has a 1 int constructor to set the
// private member, then something like this works.
//
MySubclass(int x) : MyBaseclass(x) {}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
John3136
  • 28,809
  • 4
  • 51
  • 69
  • The answer turns out [way simpler](http://stackoverflow.com/questions/36994671/can-a-private-base-class-member-variable-be-modified-in-a-derived-class#comment61543368_36994671), but the question is bad in 1st place. – πάντα ῥεῖ May 03 '16 at 02:50
0

I understand that's what getter and setter methods are for and what making the variable protected or public is for, but assuming you can't modify the base class, is there any alternate way to set it?

Sure. The alternative way instead of using setter functions in the derived class constructor body, is to use an appropriate constructor from your derived class to call an initializing constructor of the base class (as you state it exists in your comment):

class Base {
public:
    Base(int x, int y) : x_(x), y_(y) {}
private:
    int x_;
    int y_;
};

class Derived : public Base {
public:
    Derived() : Base(15,42) {}
           // ^^^^^^^^^^^^^
}:

See more details about the member initializer list.

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

Yes, we can do so by calling a getter function from the base class. Here is an example:

#include<iostream>
using namespace std;

class A{
    int x;
public:
    void setx(int x1) {
        x = x1;
    }

    int getx() {
        return x;
    }

};

class B: public A {

};

int main() {
    B b;
    b.setx(1000);
    cout << b.getx();
    return 0;
} 
Papipone
  • 1,083
  • 3
  • 20
  • 39
user20913
  • 49
  • 9