I want to create two base class that cannot be instantiated. I wrote the code as below, but it gave a compile error.
class Test {
std::string x_;
public:
Test(std::string x): x_{x} {}
};
class BaseCompartment {
Test x_;
protected:
BaseCompartment(Test x): x_{x} {}
virtual ~BaseCompartment() = 0;
const Test& getTest1() const {
return x_;
}
};
class BaseTempCompartment: public BaseCompartment {
Test y_;
protected:
BaseTempCompartment(Test x, Test y): y_{y}, BaseCompartment(x) {}
virtual ~BaseTempCompartment() = 0;
const Test& getTest2() const {
return y_;
}
};
class XTempCompartment: public BaseTempCompartment {
public:
XTempCompartment(Test x, Test y): BaseTempCompartment(x, y) {}
~XTempCompartment() {}
};
int main() {
XTempCompartment x {Test("1"), Test("2")};
return 0;
}
I'm sure there are tons of errors in the code above.
The BaseCompartment and BaseTempCompartment
classes contain a variable(Test).
I want to use these Test classes
in the XTempCompartment
class by calling getTest1() and getTest2()
.
My goal is not to let BaseCompartment and BaseTempCompartment
classes be instantiated.
Do I have to call it in abstract like below to create the Test class
?
BaseCompartment(Test x): x_{x} {}
Also Is the correct usage for getting test variables like below?
const Test& getTest1() const {
return x_;
}
How can I make the above code executable?
How can I write the above code better for these purposes?