1

我正在尝试创建一个基类,它是一个模板类,并接受某个类作为模板。这个基类在另外两个类的父类中,它们本身就是最终类的父类。因此,除了根基类是模板类之外,我在 C++ 中遇到了传统的菱形问题。在这里使用虚拟继承并不能完全解决问题。

编辑:包括一些代码(和更正的错别字)

#include<iostream>
#include<string>

using namespace std;

template<class TemplateT>
class MainMaster {
    protected: 
        std::string name;
        int number;
        TemplateT variableProp;

    public:
        explicit MainMaster(std::string, int);

        void setName(std::string);
        std::string getName();
        void printName();

        void setNumber(int);
        int getNumber();
        void printNumber();

        void printMasterProperties(std::string);

        void setVarProp(TemplateT);
        TemplateT getVarProp();
};    

template<class TemplateT>
MainMaster<TemplateT>::MainMaster(std::string nameIn, int numIn){
    setName(nameIn);
    setNumber(numIn);
}    

template<class TemplateT>
void MainMaster<TemplateT>::setName(std::string nameIn){ name = nameIn; }

template<class TemplateT>
std::string MainMaster<TemplateT>::getName(){ return name; }

template<class TemplateT>
void MainMaster<TemplateT>::printName(){ cout << "Master's name is " << name << endl; }

template<class TemplateT>
void MainMaster<TemplateT>::setNumber(int numIn){ number = numIn; }

template<class TemplateT>
int MainMaster<TemplateT>::getNumber(){ return number; }

template<class TemplateT>
void MainMaster<TemplateT>::printNumber(){ cout << name << "'s number is " << number << endl; }

template<class TemplateT>
void MainMaster<TemplateT>::printMasterProperties(std::string pre = ""){
    cout << pre << "Master Properties" << endl
        << pre << "-Number: " << number << endl;
}

class ChildOne: public virtual MainMaster<std::string>{
    protected:
        std::string propOne;

    public:
//      using MainMaster::MainMaster;
        ChildOne(std::string nameIn, int numIn, std::string p1);
        void printName();
        void setPropOne(std::string);
        std::string getPropOne();

        void printOnesProps(std::string);
};    

ChildOne::ChildOne(std::string nameIn, int numIn, std::string p1): MainMaster<std::string>(nameIn,numIn){
    setPropOne(p1);
}

void ChildOne::printName(){ cout << "ChildOne's name is " << name << endl; }

void ChildOne::setPropOne(std::string propIn){ propOne = propIn; }

std::string ChildOne::getPropOne(){ return propOne; }

void ChildOne::printOnesProps(std::string pre = ""){
    printMasterProperties("-");
    cout << pre << "One Properties" << endl
        << pre << "-PropOne: " << propOne << endl;
}    

class ChildTwo: public virtual MainMaster<int>{
    protected:
        std::string propTwo;

    public:
        ChildTwo(std::string nameIn, int numIn, std::string p2);
        void printName();

        void setPropTwo(std::string);
        std::string getPropTwo();

        void printTwosProps(std::string);
};

ChildTwo::ChildTwo(std::string nameIn, int numIn, std::string p2): MainMaster<int>(nameIn,numIn){
    setPropTwo(p2);
}

void ChildTwo::printName(){
    cout << "ChidTwo's name is " << name << endl;
}

void ChildTwo::setPropTwo(std::string propIn){ propTwo = propIn; }

std::string ChildTwo::getPropTwo(){ return propTwo; }

void ChildTwo::printTwosProps(std::string pre = ""){
    printMasterProperties("-");
    cout << pre << "Two Properties" << endl
        << pre << "-PropTwo: " << propTwo << endl;
}

class FinalChild: public ChildOne, public ChildTwo{
    protected:
        double finalProp;

    public:
        FinalChild(std::string nameIn, int num, std::string prop1 ,std::string prop2, double pFinal);
        void printFinalProps(std::string);
};

FinalChild::FinalChild(std::string nameIn, int num, std::string prop1 ,std::string prop2, double pFinal):
ChildOne(nameIn, num, prop1),
ChildTwo(nameIn, num, prop2),
MainMaster(nameIn, num){
    finalProp = pFinal; 
}

void FinalChild::printFinalProps(std::string pre = ""){
    printMasterProperties("-");
    cout << pre << name << "'s Final Properties" << endl;
    cout << pre << "-Number: " << number << endl;
    cout << pre << "-PropOne: " << propOne << endl;
    cout << pre << "-PropTwo: " << propTwo << endl;
    cout << pre << "-finalProp " << finalProp << endl;
}

int main () {
    MainMaster<char> master("Master", 0);
    ChildOne child1("Child1",1,"P1One");
    ChildTwo child2("Child2",2,"P2Two");
    FinalChild finalC("FinalChild", 3, "P1Final", "P2Final", 3.0);

    master.printMasterProperties();
    child1.printOnesProps();
    child2.printTwosProps();
    finalC.printFinalProps();


}
4

0 回答 0