I want to make an abstract class, A that will be subclassed by Class B and Class C such that they will all use the same methods in the defined abstract class (B and C are A-able classes).
I have another class, Z, that will contain an array of A-able classes. I would like for it to have a function that allows it to swap between B and C in that array (ie. calling initializer/member function with an argument).
The below example, while not being exactly like what I'm describing above (not using abstract classes), showcases the same issue I'm running into: I'm unable to set the array to the correct subclass, since it's complaining that it was initialized as the parent class.
However, this should be possible to do right? What am I missing here?
#include <iostream>
#include <array>
class BaseItem {
protected:
std::string name;
BaseItem(const std::string & name) : name(name) {};
virtual void printName();
virtual ~BaseItem() = default;
};
class Item1: public BaseItem {
public:
using BaseItem::name;
Item1() : BaseItem("Book1") {}
void printName() {
std::cout << "1" << name;
}
};
class Item2: public BaseItem {
public:
using BaseItem::name;
Item2() : BaseItem("Book2") {}
void printName() {
std::cout << "2" << name;
}
};
class Library {
public:
std::array<BaseItem, 2> books;
void setToItem2() {
for (size_t i = 0; i < books.size(); i++) {
books[i] = new Item2();
}
}
void setToItem1() {
for (size_t i = 0; i < books.size(); i++) {
books[i] = new Item1();
}
}
void printBooks() {
for (auto& entry: books) {
entry->printName();
}
}
};
int main() {
Library a;
a.setToItem1();
a.printBooks();
a.setToItem2();
a.printBooks();
return 0;
}
Edit: Cleaned up a bit, also adding error message below:
prog.cpp: In member function ‘void Library::setToItem2()’:
prog.cpp:36:31: error: no match for ‘operator=’ (operand types are ‘std::array<BaseItem, 2>::value_type’ {aka ‘BaseItem’} and ‘Item2*’)
Edit2: Made the example code more representative of what I want to implement, utilizing code help from some of the existing answers.
Current potential solutions:
Evict books and pass in the correct subclass. This is currently what I'm going with. Just don't know if there is anything that can make this look cleaner (ie. all the casting looks a bit messy).
Make books a variant. The code looks cleaner here, but if I'm to extend to Item3, Item4, etc. I'll have to increase the variant to include all those subtypes, which IMHO defeats part of the purpose of making this "interface" (of course, we still get to inherit some shared things, but I'd like to not have to keep adding new classes into variant).
For now, I'm going to just do 1. But please let me know if there is something better.
Like other comments, if you store a vector of superclass by value, say vector<A>, as the vector allocates the memory, in addition to other information that vector stores, it will allocate sizeof(A)*NumOfElement(vector<A>) for storage. As subclasses, say B need more space than A, object slicing will occur. My suggestion is, instead of storing the class as value, store those as reference. ex)vector<shared_ptr<A>>. As the size of the pointer is same, this will allow to store A's subclasses. Oh, do not forget to define its virtual destructor!
Suggested code:
#include <iostream>
#include <vector>
#include <memory>
class Item {
public:
Item() : name("Book1") {}
std::string name;
virtual void f1() {/* Your Implementation here or make it pure virtual */};
virtual ~Item() = 0;
};
class Item2 : public Item {
public:
Item2() { name = "Book2"; }
//std::string name; //Hides base class name
void f1() override {/* Your Implementation here */};
~Item2() = default;
};
class Library {
public:
std::vector<std::shared_ptr<Item>> books;
void setToItem2() {
books.emplace_back(std::dynamic_pointer_cast<Item>(std::shared_ptr<Item2>(new Item2()))); //If you wish, use loop here
books.emplace_back(std::dynamic_pointer_cast<Item>(std::shared_ptr<Item2>(new Item2())));
}
void printBooks() {
for (auto& entry : books) {
std::cout << entry->name;
}
}
};
int main() {
Library a;
a.printBooks();
return 0;
}
The blessed way to store polymorphic instances in a container is to use std::unique_ptr. The container is still the sole owner of the object, but that pattern does not suffer the object slicing problem.
Furthermore your class hierarchy is weird: an Item2 instance will contain two versions of name. One (not directly accessible) in its Item base class and one directly accessible. It should at least be:
class Item2 : public Item {
public:
using Item::name;
Item2() {
name = "Book2";
}
};
But at construction time, name will first receive "Book1" at the base class initialization time, and then "Book2". So the normal way would be to build a base class like:
class BaseItem {
protected:
std::string name;
BaseItem(const std::string & name) : name(name) {};
virtual ~BaseItem() = default;
};
class Item: public BaseItem {
public:
using BaseItem::name;
Item() : BaseItem("Book1") {}
};
You can now build your Library class:
class Library {
public:
std::array<std::unique_ptr<BaseItem>, 2> books;
void printBooks() {
for (auto& entry : books) {
std::cout << entry->name;
}
}
};
Alternatively if you want to stick to a swapping pattern, you should use a variant:
class Library {
public:
std::variant<std::array<Item, 2>, std::array<Item2, 2> > books = std::array<Item, 2>();
void setToItem2() {
books = std::array<Item2, 2>();
}
void printBooks() {
auto *b = std::get_if< std::array<Item, 2> >(&books);
if (nullptr != b) {
for (auto& entry : *b) {
std::cout << entry.name << "\n";
}
}
else {
auto* b2 = std::get_if< std::array<Item2, 2> >(&books);
for (auto& entry : *b2) {
std::cout << entry.name << "\n";
}
}
}
};
int main() {
Library a;
a.printBooks();
a.setToItem2();
a.printBooks();
return 0;
}
Related
I have six different classes that are all derived from an abstract base class named "Piece".
When looping through a vector of pointers to Piece as such:
std::vector<shared_ptr<Piece>>
Is there an efficient way to get the type of derived class that each pointer is pointing to?
Obviously I can just do something like this:
class Piece
{
public:
virtual std::string getType() = 0;
}
class Rook : public Piece
{
public:
std::string getType() override
{
return "Rook";
}
}
class Pawn : public Piece
{
public:
std::string getType() override
{
return "Pawn";
}
}
for (std::shared_ptr<Piece> p: std::vector<std::shared_ptr<Piece>>)
{
if (p->getType() == "Rook")
{
//Do something
}
else if(p->getType() == "Pawn")
{
//Do something else
}
}
But it seems like there would be a better way to do this?
Slightly more efficient would be an enum rather than a string, as these can be compared slightly faster (they're one integer!).
But it's ideal to try to avoid this need, if you can.
This:
if (p->getType() == "Rook")
{
//Do something
}
else if(p->getType() == "Pawn")
{
//Do something else
}
should just be this:
p->doThing();
… where doThing is a virtual function that does "the right thing" for whatever class it's implemented in.
Failing that, your pattern is not unusual.
As another answer shows, using an enum would be more efficient than using a string. But at the cost of having to maintain that enum if you ever decide to add more Piece-derived classes in the future (though, Chess only has a few distinct pieces, so I'm sure that will not happen).
Otherwise, have a look at dynamic_cast or std::dynamic_pointer_cast instead of a virtual method, eg:
class Piece
{
public:
virtual ~Piece() = default;
...
};
class Rook : public Piece
{
public:
...
void doSomething();
};
class Pawn : public Piece
{
public:
...
void doSomethingElse();
};
...
std::vector<std::shared_ptr<Piece>> pieces;
...
for (auto p : pieces)
{
if (Rook *rk = dynamic_cast<Rook*>(p.get()))
// or: if (auto rk = std::dynamic_pointer_cast<Rook>(p))
{
rk->doSomething();
}
if (Pawn *pn = dynamic_cast<Pawn*>(p.get()))
// or: if (auto pn = std::dynamic_pointer_cast<Pawn>(p))
{
pn->doSomethingElse();
}
}
But, as others have stated, this is not a good class design. This would be better handled by using a single virtual method for the actual work instead, eg:
class Piece
{
public:
virtual ~Piece() = default;
virtual void someAction() = 0;
...
};
class Rook : public Piece
{
public:
...
void someAction() override {
// do something ...
}
};
class Pawn : public Piece
{
public:
...
void someAction() override {
// do something else ...
}
};
...
std::vector<std::shared_ptr<Piece>> pieces;
...
for (auto p : pieces)
{
p->someAction();
}
Using enums and a function in base.
Untested concept code
enum class PieceType { Rock, Pawn, etc. };
class Piece {
enum id = -1; // no such piece
public:
virtual std::string getType() = 0;
enum PieceType GetId() {
return id;
}
}
class Rook : public Piece {
public:
Rock() : id(PieceType::Rock) {}
std::string getType() override {
return "Rook";
}
}
If it needs to be fast then this would do, if you just need to display it for one player once in a while, your method is fine.
Assuming that "Rook" and "Pawn" don't know about each other (like they are of different libraries) and that they also know nothing about your "Do something" code nor you have a vector of std::variant, it's not an easy task as you need to use some double-dispatch method.
The common solution is to create visitor interface per each derived class and visitor accept method.
So you have your Piece.cpp file:
struct Piece;
struct IPieceVisitor {
virtual void visit(Piece*) = 0;
};
struct Piece {
virtual void accept(IPieceVisitor *v) {
v->visit(this);
}
};
And Rook.cpp with all others implementing classes should have:
struct Rook;
struct IRookVisitor: public virtual IPieceVisitor {
virtual void visit(Rook*) = 0;
};
struct Rook : public Piece {
void accept(IPieceVisitor *v) override {
if(IRookVisitor *rv = dynamic_cast<IRookVisitor*>(v) {
return rv->visit(this);
}
Piece::accept(v); // consider that it may also be derived from some SpecialPiece
}
};
Now you can write your "Do something" visitor:
struct GratherKnownPiecesVisitor:
public IRookVisitor, public IPawnVisitor {
void visit(Rook* r) override{
m_knownPieces.push_back(r);
}
void visit(Pawn* p) override {
m_knownPieces.push_back(p);
}
void visit(Piece* p) override {
std::cerr << "Unknown piece!" << std::endl;
}
std::vector<std::variant<Rook*,Pawn*>> m_knownPieces;
};
Now you can smoothly run over the unknown container of base class pointers:
GratherKnownPiecesVisitor knownTypesVisitor;
for (std::shared_ptr<Piece> p: pieces) {
p->accept(&knownTypesVisitor);
}
The easier and perhaps faster solution is to use hash map indexed with type information:
std::unordered_map<std::type_index, std::functinon<void(Piece*)>>
But that way if someone create SpecialRook class, we cannot know that it is derived from our Rook class.
The fastest solution is to assign each object a value of your enum and then switch case to the type you want.
This question already has answers here:
Why doesn't polymorphism work without pointers/references?
(6 answers)
What does slicing mean in C++?
(4 answers)
How to use polymorphism to access derived class vector member from base class?
(1 answer)
Closed 3 years ago.
I have several classes that inherit from one main class. For the sake of simplicity, I have over-simplified the class definitions to make it short and direct to the point.
animal.h
main class which all other classes inherit from:
class Animal {
protected:
string name;
public:
Animal(string name);
virtual string toString() { return "I am an animal"; }
};
bird.h
class Bird: public Animal {
private:
bool canFly;
public:
Bird(string name, bool canFly = true)
: Animal(name) // call the super class constructor with its parameter
{
this->canFly = canFly;
}
string toString() { return "I am a bird"; }
};
indect.h
class Insect: public Animal {
private:
int numberOfLegs;
public:
Insect(string name, int numberOfLegs) : Animal(name) {
this->numberOfLegs = numberOfLegs;
}
string toString() { return "I am an insect."; }
};
Now, I need to declare a vector<Animal> that will hold several instances of each inherited class.
main.cpp
#include <iostream>
#include "animal.h"
#include "bird.h"
#include "insect.h"
// assume that I handled the issue of preventing including a file more than once
// using #ifndef #define and #endif in each header file.
int main() {
vector<Animal> creatures;
creatures.push_back(Bird("duck", true));
creatures.push_back(Bird("penguin", false));
creatures.push_back(Insect("spider", 8));
creatures.push_back(Insect("centipede",44));
// now iterate through the creatures and call their toString()
for(int i=0; i<creatures.size(); i++) {
cout << creatures[i].toString() << endl;
}
}
I expected the following output:
I am a bird
I am a bird
I am an insect
I am an insect
but I got:
I am an animal
I am an animal
I am an animal
I am an animal
I know this has to do with the line 'vector creatures;. It is calling the constructor for Animal. But my intention is to tell the compiler, that this creaturespoints to an array ofAnimalinherited classes, might beBirdmight beinsect, the point is: they all implement their own unique respective version of toString()`.
What can I do to declare a polymorphic array of objects that are inherited from the same ancestor?
You cannot use a value semantic (read about object slicing). You must use pointers.
Example:
#include <iostream>
#include <memory>
#include <string>
#include <vector>
class Animal
{
protected:
std::string name;
public:
Animal(std::string name) : name(name)
{
}
virtual std::string toString()
{
return "I am an animal";
}
virtual ~Animal()
{
}
};
class Bird : public Animal
{
private:
bool canFly;
public:
Bird(std::string name, bool canFly = true) : Animal(name) // call the super class constructor with its parameter
{
this->canFly = canFly;
}
std::string toString()
{
return "I am a bird";
}
};
class Insect : public Animal
{
private:
int numberOfLegs;
public:
Insect(std::string name, int numberOfLegs) : Animal(name)
{
this->numberOfLegs = numberOfLegs;
}
std::string toString()
{
return "I am an insect.";
}
};
int main()
{
std::vector<std::unique_ptr<Animal>> creatures;
creatures.emplace_back(new Bird("duck", true));
creatures.emplace_back(new Bird("penguin", false));
creatures.emplace_back(new Insect("spider", 8));
creatures.emplace_back(new Insect("centipede", 44));
// now iterate through the creatures and call their toString()
for (int i = 0; i < creatures.size(); i++)
{
std::cout << creatures[i]->toString() << std::endl;
}
}
prints:
I am a bird
I am a bird
I am an insect.
I am an insect.
I also recommend reading about Sean parent Run Time Polymorphism. The idea is as follows:
#include <iostream>
#include <memory>
#include <string>
#include <vector>
class Animal
{
public:
struct Interface
{
virtual std::string toString() const = 0;
virtual ~Interface() = default;
};
std::shared_ptr<const Interface> _p;
public:
Animal(Interface* p) : _p(p)
{
}
std::string toString() const
{
return _p->toString();
}
};
class Bird : public Animal::Interface
{
private:
std::string _name;
bool _canFly;
public:
Bird(std::string name, bool canFly = true) : _name(name), _canFly(canFly)
{
}
std::string toString() const override
{
return "I am a bird";
}
};
class Insect : public Animal::Interface
{
private:
std::string _name;
int _numberOfLegs;
public:
Insect(std::string name, int numberOfLegs)
: _name(name), _numberOfLegs(numberOfLegs)
{
}
std::string toString() const override
{
return "I am an insect.";
}
};
int main()
{
std::vector<Animal> creatures;
creatures.emplace_back(new Bird("duck", true));
creatures.emplace_back(new Bird("penguin", false));
creatures.emplace_back(new Insect("spider", 8));
creatures.emplace_back(new Insect("centipede", 44));
// now iterate through the creatures and call their toString()
for (int i = 0; i < creatures.size(); i++)
{
std::cout << creatures[i].toString() << std::endl;
}
}
Problem is with creatures.push_back(Bird("duck", true));
You are creating a Bird object and copying that in the Animal object.
One way is to create objects dynamically so that correct function call can resolve using vtable.
Modify this part of your code and it will work fine.
vector<Animal *> creatures;
creatures.push_back(new Bird("duck", true));
creatures.push_back(new Bird("penguin", false));
creatures.push_back(new Insect("spider", 8));
creatures.push_back(new Insect("centipede",44));
Edit: Make sure to release the memory before creatures goes out of scope.
C++ objects are values with specific types. This differs from many languages where variables always hold references to objects, so they can hold references to derived objects just as easily.
If you copy an instance if a derived class onto an obhect of a base class, you get slicing: only the base class data is copied, and the type of the assignee is still that of the base class.
To achieve polymorphic behaviour in C++ you need to either use std::variant to specify the allowed possibilities, in which case the object will hold one of the options, and will switch type between them when assigned to, or you need to use a pointer to the base class, which can hold a pointer to any derived type, but you must then be wary of memory leaks. You do need to use std::visit or std::get to retrieve the values though.
If you are going to use pointers you should always use std::shared_ptr or std::unique_ptr to manage the objects in order to avoid memory leaks.
Code with variant:
int main() {
vector<std::variant<Bird,Insect>> creatures;
creatures.push_back(Bird("duck", true));
creatures.push_back(Bird("penguin", false));
creatures.push_back(Insect("spider", 8));
creatures.push_back(Insect("centipede",44));
// now iterate through the creatures and call their toString()
for(int i=0; i<creatures.size(); i++) {
cout << std::visit([](auto const& creature){
return creature.toString();
},creatures[i]) << endl;
}
}
Code with pointers:
int main()
{
std::vector<std::unique_ptr<Animal>> creatures;
creatures.emplace_back(std::make_unique<Bird>("duck", true));
creatures.emplace_back(std::make_unique<Bird>("penguin", false));
creatures.emplace_back(std::make_unique<Insect>("spider", 8));
creatures.emplace_back(std::make_unique<Insect>("centipede", 44));
// now iterate through the creatures and call their toString()
for (int i = 0; i < creatures.size(); i++)
{
std::cout << creatures[i]->toString() << std::endl;
}
}
Code with std::shared_ptr is equivalent: just replace unique with shared everywhere.
I would like to have to counters for each class type that that was ever instantiated. As a starting point someone sugested this approach as an example:
class Person
{
public:
Person() {
objects.push_back(this);
}
virtual ~Person() {
objects.erase(this);
}
static void print_types()
{
for (auto pers : container)
{
std::cout << typeid(*pers).name() << "\n";
}
}
private:
static std::set<const Person*> objects;
};
class Employee : public Person
{
};
class Employee2 : public Employee
{
};
Each time one of the classes is instatiated I keep track of the objects and I can use print_types() to know how many of which type I've created so far. Notice that Employee2 inherits from Employee and not from Person (i need this to work for chain inheritance)
I would like to extend this so that I have two counters per type: created and alive. The problem is that you can't easily mantain the counters from the constructor/destructor of the base class, Person, because typeid(*this) will return the base class type when called from constructor/destructor.
Another suggestion was to use CRTP pattern but this doesn't work when you use chained inheritance.
Is there another way to implement such counters ?
I just played around a bit. Maybe this helps you. It always prints the value of the right class (not the base class).
But it's practically the same^^.
Header:
#include <set>
#include <string>
class observeable;
class observer
{
public:
observer() = delete;
static void print();
static std::set< observeable* > items;
};
class observeable
{
public:
observeable();
virtual ~observeable();
virtual std::string get_typeid();
};
Source:
std::set< observeable* > observer::items;
void observer::print()
{
std::cout << "Called" << std::endl;
for( auto item : items )
std::cout << item->get_typeid() << std::endl;
}
observeable::observeable()
{
observer::items.insert( this );
}
observeable::~observeable()
{
observer::items.erase( this );
}
std::string observeable::get_typeid()
{
return std::string( typeid(*this).name() );
}
Main:
#include <memory>
class A : observeable
{};
class B : A
{};
class C : observeable
{};
int main()
{
A a;
B b;
observer::print(); // A B present
{
C d;
}
observer::print(); // no C present
auto d_heap = std::shared_ptr<C>( new C() );
observer::print(); // C present
return 0;
}
Sorry for the convoluted title of my question, conceptually it is quite simple but I can't find any good design to do it.
I have a base class accessible by the end user :
class A {
private:
// m is a functor
Base* m;
};
class Base {
public:
virtual void someInterface();
};
class DerivedT1 : public Base {
public:
virtual void someInterface()
{
some_parameter++;
}
private:
int some_parameter; // how to set?
};
class DerivedT2 : public Base {
public:
virtual void someInterface()
{
some_other_parameter += a_third_parameter;
}
private:
double some_other_parameter; // how to set?
double a_third_parameter; // how to set?
};
And I am trying to find the most generic way to set some_parameter and some_other_parameter from A's public interface.
I have thought of giving a number to my parameters but this sounds really ugly.
Is there any beautiful, object-oriented way to do this ?
you want to use A's public interface to set derived class parameters:
you can define a public function In A, which have a Base* parameter:
class A
{
public:
void setter(const Base *p);
{
m = p;
}
};
if you want to set Drived1 you can define a object of Derived1, can pass it to setter;
I think you want to pass value using A's public function, you must know the type of pointer of Base*,so you can pass value by the constructor of Derived1 or Derived2!
I nothing else works, you could always use a dynamic cast:
DerivedT1 *d1 = dynamic_cast<DerivedT1>(m);
if (d1 != nullptr)
{
// do something with derived 1
}
else
{
DerivedT2 *d2 = dynamic_cast<DerivedT2>(m);
if (d2 != nullptr)
{
// do something with derived 2
}
}
But if you need that, it's usually a sign that there is something wrong with your design.
If you want to do something along these lines
A a; a.setAlgorithmFamily(Algorithm::Type1);
a.getAlgorithmImplementation().setSomeParameter(34);
This is a quick and kind of dirty example of how you could do it. A::setAlgorithmType is basically a factory pattern in it's simplest form.
nclude <iostream>
using namespace std;
class Algorithm {
public:
virtual void setParameter(int value) = 0;
};
class AlgoX : public Algorithm {
int mX;
public:
void setParameter(int value) {
cout <<"Setting X to " <<value <<endl;
mX = value;
}
};
class AlgoY : public Algorithm {
int mY;
public:
void setParameter(int value) {
cout <<"Setting Y to " <<value <<endl;
mY = value;
}
};
class A {
public:
void setAlgorithmType(std::string type) {
cout <<"Now using algorithm " <<type <<endl;
if(type == "X")
mAlgorithm = new AlgoX();
else if(type == "Y")
mAlgorithm = new AlgoY();
}
Algorithm* getAlgorithmImplementation() { return mAlgorithm; }
private:
Algorithm* mAlgorithm;
};
int main(int argc, char** argv) {
A a;
a.setAlgorithmType("X");
a.getAlgorithmImplementation()->setParameter(5);
return 0;
}
This gives:
Now using algorithm X
Setting X to 5
Imagine I have a class 'BaseA' that contains a collection of items 'ItemA'.
Now I want to extend 'BaseA' to add extra capabilities, so I derive 'DerivedA' from 'BaseA'.
One characteristic of 'DerivedA' is that it has to handle more sophisticated 'DerivedITemA' items instead of 'ItemA' ones.
class BaseA {
protected:
vector<ItemA> x;
void m1(int i) { x.m1(i); }
};
class ItemA {
protected:
void m1(int i) { ... }
};
class DerivedItemA : public ItemA {
void m2(int i) { ... }
};
Now I would like to handle something of this sort:
class DerivedA : public BaseA {
vector<DerivedItemA> x;
void m2(int i) { x[i].m2(); }
};
I.e. have my Derived class handle derived items. The above definition of x is incorrect as it clashes with the one in BaseA. But the idea is I want to be able to reuse all methods in BaseA that handle x as long as they deal with ItemA elements and have the extended methods in DerivedA to handle the extra intricacies of DerivedItemA type of data
Any suggestion? My current thoughts are in the lines of defining a new datatype for x (VectorOfItemA for instance) and derive from it VectorOfDerivedItemA. I wonder if there is a simpler / better solution.
Thanks
I believe you need to have pointers in your vectors in order to handle this. I'm a little confused what value to pass to m1 and m2 since i appears to be an index, but here's my guess:
class BaseA {
protected:
vector<ItemA*> x;
void m1(int i) { x[i]->m1(i); }
};
class ItemA {
protected:
void m1(int i) { ... }
};
class DerivedItemA : public ItemA {
void m2(int i) { ... }
};
class DerivedA : public BaseA {
vector<DerivedItemA*> y; //don't shadow the base class vector!
void m2(int i) { y[i]->m2(i); }
};
Then, when you add an item in DerivedA, add it to both x and y. That way BaseA can do it's thing to the pointer in x and DerivedA can do its thing on the pointer in y.
Edit: you'll also need to provide a virtual method for adding items otherwise you might get things added to BaseA.x that don't get added to DerivedA.y.
Do you own all the classes? If so, you can refactor into a template base class instead.
template <typename ITEM>
class BaseT {
protected:
vector<ITEM> x;
void m1(int i) { x[i].m1(); }
};
typedef BaseT<ItemA> BaseA;
class DerivedA: public BaseT<DerivedItemA> {
void m2(int i) { x[i].m2(); }
};
If you intend to re-use code that takes BaseA to also accept a DerivedA, then you may need to modify them to be template functions/classes as well.
Otherwise, you will need some kind of "polymorphic" base object for the vector. You can look at Retrieve data from heterogeneous std::list (or my follow up question: unique_ptr member, private copy constructor versus move constructor) for one such approach.
As an alternative to a polymorphic item, you can define an interface for your base.
class BaseI {
protected:
virtual void m1(int) = 0;
//... other interfaces
public:
virtual ~BaseI () {}
//... other public interfaces
};
template <typename ITEM>
class BaseT : public BaseI {
protected:
vector<ITEM> x;
void m1(int i) { x[i].m1(); }
//...implement the other interfaces
};
//...
Now, code that takes a BaseA needs to be refactored to take a BaseI instead. That new code will be able to accept a DerivedA as well.
You may try to use Curiously Recurring Template Pattern - CRTP:
live demo
#include <iostream>
#include <ostream>
#include <vector>
using namespace std;
struct Item
{
void m1(int i)
{
cout << "m1(" << i << ")" << endl;
}
};
struct DerivedItem : Item
{
void m2(int i)
{
cout << "m2(" << i << ")" << endl;
}
};
template<typename Derived>
struct IBase
{
void m1(int i)
{
for(auto &&z : static_cast<Derived*>(this)->x)
{
z.m1(i);
}
}
};
template<typename Derived>
struct IDerivedBase: IBase<Derived>
{
void m2(int i)
{
for(auto &&z : static_cast<Derived*>(this)->x)
{
z.m2(i);
}
}
};
struct Base : IBase<Base>
{
vector<Item> x;
};
struct DerivedBase : IDerivedBase<DerivedBase>
{
vector<DerivedItem> x;
};
int main()
{
Base b;
b.x.resize(3);
DerivedBase d;
d.x.resize(1);
b.m1(11);
d.m1(22);
d.m2(33);
}
Output is:
m1(11)
m1(11)
m1(11)
m1(22)
m2(33)
Vector will contain either all elements as ItamA in BaseA instantiations or all elements of DerivedItemA in DerivedA instantiaions. There is no need to mix.
There is no any mix at this approach:
Base has only vector<Item> providing m1 method
DerivedBase has only vector<DerivedItem> providing m1 and m2 methods.
However, without knowing real usage pattern - it is hard to guess what you need. Maybe for your case two standalone vectors would be enough:
vector<Item> x1;
vector<DerivedItem> x2;
and just define stand-alone functions for them.