Sorry bothering you with such an 'easy' question. I am trying to keep an overview over my code, so I want the following nice and clean, here is my condensed code:
class field_t {
public:
field_t(fieldmap_t* fieldmap);
~field_t();
private:
fieldmap* fieldmap;
virtual int add_self_to_set(){ std::cout << "ERROR"; std::exit(0); };
};
//constructor
field_t::field_t(fieldmap * fieldmap){
this->fieldmap=fieldmap;
this->add_self_to_list();
};
class area_t : public field_t {
public:
// constructors are not inherited
area_t(fieldmap_t* fieldmap) : field_t(fieldmap);
private:
virtual int add_self_to_set(); //virtual because their will be further child classes.
};
int area_t::add_self_to_set() {
this->fieldmap->area_list.insert(this);
return 1;
};
Okay, this code should work (hope I missed no ; or so).
What is the problem? As the constructor of area_t calls
field_t::field_t(fieldmap)
it constructs (of course) a field_t object
, which will say 'Error' and exit()
;.
What I wanted is that the replaced area_t::add_self_to_set()
function is called in the constructor (that's why I declared it virtual in field_t
), but this doesn't work. This seems logic to me, but I don't know how to implement it properly. I need to insert the class into another list (a std::set
) for each class type.
When I noticed the problems, I also thought about a different way. I could add the add_self_to_list()
call to the area_t
constructor by directly calling
area_t(fieldmap_t* fieldmap) : field_t(fieldmap){this->fieldmap->area_list.insert(this);};
and leave out all those virtual add_self_to
list-stuff. But this leads to the next problem at the next child:
class entrance_t : public area_t{
entrance_t(fieldmap_t* fieldmap) : area_t(fieldmap){this->fieldmap->entrance_list.insert(this);};
};
Of course I do not want entrance_t
to appear in the are_list
!
Similar problem for the constructors.
Any ideas?? Thanks a lot for your time in advance. I guess I don't need to say, that I am beginner ^^.
Greets