I\'m new to C++ programming, but have been working in C and Java for a long time. I\'m trying to do an interface-like hierarchy in some serial protocol I\'m working on, and kee
If you're just looking to do an interface, you don't need new/delete. Just remove the "virtual" from the base class destructor, and make sure the derived class has an implementation of __cxa_pure_virtual().
Here is a compilable example. (I removed the returns to keep things simple, but it works just fine with them.)
In PacketWriter.h
class PacketWriter {
public:
virtual void nextByte() = 0;
protected:
~PacketWriter() {}
};
In StringWriter.h
#include "PacketWriter.h"
class StringWriter : public PacketWriter {
public:
StringWriter(const char* message);
void nextByte();
};
In StringWriter.cpp
#include "StringWriter.h"
// Definition of the error function to call if the constructor goes bonkers
extern "C" void __cxa_pure_virtual() { while (1); }
StringWriter::StringWriter(const char* message)
{
// constructor code here
}
void StringWriter::nextByte()
{
}
Compile with avr-g++ StringWriter.cpp