I am posting my code below as tested, with the respective outputs shown in comments after the "cout"-statements, and my QUESTIONS as to the output that I do not understand indicated by "--> WHY". I am completely at a loss and I apologize in advance for my assumed stupidity that appears to prevent me from understanding what is going on.
#include <experimental/filesystem>
#include <iostream>
#include <vector>
using namespace std;
struct S
{
struct BASE
{
static double testval;
virtual void set_testval(double val) {testval = val;}
virtual double get_testval() {return testval;}
};
template<typename T>
struct DerivedTemplate : public BASE
{
static double testval;
void set_testval(double val) {testval = val;}
double get_testval() {return testval;}
};
struct DERIVED_01 : DerivedTemplate<DERIVED_01>
{
//...
};
struct DERIVED_02 : DerivedTemplate<DERIVED_02>
{
//...
};
struct DERIVED_03 : DerivedTemplate<DERIVED_03>
{
//...
};
vector<unique_ptr<BASE> > DERIVED_TYPES;
S();
}; // END struct S
S::S()
{
DERIVED_TYPES.resize(4);
}
double S::BASE::testval = 0;
template <typename T>
double S::DerivedTemplate<T>::testval = 1;
S s;
int main()
{
// Assign pointers to objects of derived structs to fields in vector of unique_ptr of base struct
{
unique_ptr<S::DERIVED_01> DERIVED (new S::DERIVED_01());
s.DERIVED_TYPES[0] = move(DERIVED);
}
{
unique_ptr<S::DERIVED_02> DERIVED (new S::DERIVED_02());
s.DERIVED_TYPES[1] = move(DERIVED);
}
{
unique_ptr<S::BASE> BASE (new S::BASE());
s.DERIVED_TYPES[2] = move(BASE);
}
{
unique_ptr<S::DERIVED_03> DERIVED (new S::DERIVED_03());
s.DERIVED_TYPES[3] = move(DERIVED);
}
cout << s.DERIVED_TYPES[0]->testval << endl; // Output: 0
cout << s.DERIVED_TYPES[0]->get_testval() << endl; // Output: 1 --> WHY is the output of this line "1" while that of the prior line was "0"?
// I assumed to be accessing the same member variable of the same object in both cases
cout << endl;
cout << s.DERIVED_TYPES[1]->testval << endl; // Output: 0
cout << s.DERIVED_TYPES[1]->get_testval() << endl; // Output: 1 --> WHY [same question]
cout << endl;
cout << s.DERIVED_TYPES[2]->testval << endl; // Output: 0
cout << s.DERIVED_TYPES[2]->get_testval() << endl; // Output: 0
cout << endl;
cout << s.DERIVED_TYPES[3]->testval << endl; // Output: 0
cout << s.DERIVED_TYPES[3]->get_testval() << endl; // Output: 1 --> WHY [same question]
cout << endl;
// Assign values to static member variables of derived struct objects
s.DERIVED_TYPES[0]->testval = 0.5;
s.DERIVED_TYPES[1]->testval = 1.5;
s.DERIVED_TYPES[2]->testval = 2.5;
s.DERIVED_TYPES[3]->testval = 2.75;
cout << s.DERIVED_TYPES[0]->testval << endl; // Output: 2.75
cout << s.DERIVED_TYPES[1]->testval << endl; // Output: 2.75
cout << s.DERIVED_TYPES[2]->testval << endl; // Output: 2.75
cout << s.DERIVED_TYPES[3]->testval << endl; // Output: 2.75 --> WHY are the outputs all "2.75"?
cout << endl;
s.DERIVED_TYPES[0]->set_testval(3.5);
s.DERIVED_TYPES[1]->set_testval(4.5);
s.DERIVED_TYPES[2]->set_testval(5.5);
s.DERIVED_TYPES[3]->set_testval(6.5);
cout << s.DERIVED_TYPES[0]->testval << endl; // Output: 5.5
cout << s.DERIVED_TYPES[0]->get_testval() << endl; // Output: 3.5
cout << endl;
cout << s.DERIVED_TYPES[1]->testval << endl; // Output: 5.5
cout << s.DERIVED_TYPES[1]->get_testval() << endl; // Output: 4.5
cout << endl;
cout << s.DERIVED_TYPES[2]->testval << endl; // Output: 5.5
cout << s.DERIVED_TYPES[2]->get_testval() << endl; // Output: 5.5
cout << endl;
cout << s.DERIVED_TYPES[3]->testval << endl; // Output: 5.5
cout << s.DERIVED_TYPES[3]->get_testval() << endl; // Output: 6.5
// Now, a "DIRECT ACCESS" of "testval" [s.DERIVED_TYPES[x]->testval]
// does not produce an output equal to the value assigned to
// s.DERIVED_TYPES[3]->testval, the last one assigned, but an
// output equal to the one assigned to the pointer pointing to the
// one object of struct BASE instead of "DERIVED_..."
// --> WHY?
cin.get();
return 0;
}