首页 > 解决方案 > C++:为什么通过 struct-template 定义的派生结构中的成员不隐藏基本结构中的相应成员变量?

问题描述

我在下面发布了经过测试的代码,在“cout”语句之后的注释中显示了相应的输出,而关于我不理解的输出的问题由“--> WHY”表示。我完全不知所措,我提前为我假设的愚蠢道歉,这似乎阻止我理解正在发生的事情。

#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;
}

标签: c++inheritancehideunique-ptrclass-template

解决方案


推荐阅读