This question already has an answer here:
- When to use virtual destructors? 16 answers
Given following class structure, I'm not able execute this line of code without program to crash at the end of the function.
For:
unique_ptr<BufferedFieldEncoder> p = make_unique<GenericFieldEncoder>();
I get:
Process finished with exit code 132 (interrupted by signal 4: SIGILL)
but this line works fine (no crash):
unique_ptr<FieldEncoder> p = make_unique<GenericFieldEncoder>();
This is how my class structure looks like
class FieldEncoder {
public:
virtual void encode(const string &data, const FieldEncoderMode &mode) = 0;
};
class BufferedFieldEncoder : public FieldEncoder {
protected:
vector<uint8_t> m_buffer;
public:
vector<uint8_t> &getBuffer() {
return m_buffer;
}
};
class GenericFieldEncoder : public BufferedFieldEncoder {
private:
const unique_ptr<Something> something; (maybe this line is necessary)
public:
explicit GenericFieldEncoder(); // initialisation of 'something'
void encode(const string &data, const FieldEncoderMode &mode) override;
};
How can I create an instance of derived class and store it in unique_ptr of abstract derived class?
unique_ptr<BufferedFieldEncoder> p = make_unique<GenericFieldEncoder>();
I'm no c++ expert as you can see :)
Aucun commentaire:
Enregistrer un commentaire