I have a stack of classes with pure virtual members, it'll be populated by derived non-abstract classes. I'm getting the error:
Error C2259 'ABC': cannot instantiate abstract class TEMP c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.16.27023\include\xmemory0 879
This error can be solved by using stack<ABC*>
, however heap memory access is slower than stack memory, so I'm thinking about rewriting the base class ABC with no pure virtual members at all.
Would there be any drawbacks from this (except the possible human error of whoever might use this code)?
Is there a way I can create a stack of classes with pure virtual members on the stack? Or, maybe, am I too paranoid about using the heap? The class (in the original code) would be accessed very frequently.
Simplified version of the code below:
#include <iostream>
#include <stack>
class ABC {
public:
ABC(int& a) : m_a(a) {}
~ABC() {}
virtual void do_something() = 0;
int m_a;
};
class DEF : public ABC {
public:
DEF(int& a) : ABC(a) {}
void do_something() override;
~DEF() {}
};
void DEF::do_something() {
std::cout << "Hi!\n";
}
int main(int argc, char* argv[]) {
int x = 123;
std::stack<ABC> s;
s.push(DEF(x));
}