// Star is in the wrong place
void Field::*data(){
return NULL;
}
// Star is in the wrong place
// void* is the wrong return type
void Field::*clone() const{
return NULL;
} The class name and scope operator go right next to the method name, after the return type. You just need to move the star over a bit. In your class you say that clone() returns a Field* too:
void *Field::data(){
return NULL;
}
Field *Field::clone() const{
return NULL;
} Make sure you include a header that defines NULL, like , or that might cause an error too.
On a design note, the entire Field class should be abstract because it's a base class. That saves you the need to implement dummy methods that don't do anything but return a useless value. ;) A good rule of thumb is to make any base class abstract and only leaf classes concrete. That way you don't have to worry about problems like slicing. Also, if you plan on using Field polymorphically, a virtual destructor is a good idea:
#include <iostream>
#include <string>
class Field{
public:
virtual void display() = 0;
virtual int edit() = 0;
virtual bool editable() const = 0;
virtual void *data() = 0;
virtual Field *clone() const = 0;
virtual ~Field() = 0 {}
};
class TextField: public Field {
std::string _text;
public:
TextField(std::string text): _text(text) {}
~TextField() {}
void display() { std::cout << _text << '\n'; }
int edit() { return 0; }
bool editable() const { return false; }
void *data() { return (void*)_text.c_str(); }
Field *clone() const { return new TextField(_text); }
};
int main()
{
Field *field1 = new TextField("Ed Rules!");
Field *field2 = field1->clone();
field1->display();
field2->display();
delete field2;
delete field1;
}