Hello everyone I have to create a class that has the following:

Your Field class is the base class from which you will derive all other Field classes.

Your Field class has the following public member functions, all of which are virtual:
* void display() - does nothing here, but will display the field in derived classes.
* int edit() - does nothing here and returns 0, but will edit the field in derived classes.
* bool editable() const - returns false here, but will return the editability of the field in derived classes.
* void *data() - returns NULL here, but will return the address of data stored in the field in derived classes.
* Field *clone() const - returns NULL here, but will create a clone of the field in derived classes.

I would like some input if I did the above correctly.

This is what is in my header file:

class Field{
    public:
        virtual void display();
        virtual int edit();
        virtual bool editable() const;
        virtual void *data();
        virtual Field *clone() const;
};

This is what I have in my cpp file:

// Field Class functions
void Field::display(){
    
}
int Field::edit(){
    return 0;
}
bool Field::editable() const{
    return false;
}
void Field::*data(){
    return NULL;
}
void Field::*clone() const{
    return NULL;
}

I am getting errors with what I currently have and decided to break it down piece by piece. I would like to know if what I did is correct, any input is highly appreciated.

Recommended Answers

All 5 Replies

Well No Wonder You are getting errors. This is because you have declared a void function and giving a return value to it.

// 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 <cstddef>, 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;
}

Thanks for that information! Although when I compile that example program I get an error saying "pure specifier on function definition" on this line:

virtual ~Field() = 0 {}

Sorry, Edward was accidentally using a Visual Studio extension. This should work:

#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;
};

Field::~Field() {}

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;
}

I see thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.