E
ex_ottoyuhr
I have a situation more or less as follows, and it's causing me no end
of trouble; I'd appreciate anyone's advice on the matter...
Given these classes:
class BedrockCitizen { ... };
class Fred : public BedrockCitizen { ... };
class Wilma : public BedrockCitizen { ... };
class Barney : public BedrockCitizen { ... };
..
..
..
class TownOfBedrock { ... };
What I'd like to be able to do is to create a means of storing any
number of BedrockCitizens of any of the child classes in class
TownOfBedrock, without too much inconvenience. Unfortunately, while
that can be done by having a data structure indexing
pointers-to-BedrockCitizen, and downcasting as appropriate, there's a
heck of a lot of inconvenience involved.
At the present, I'm badly bogged down in the attendant memory
management in classes using BedrockCitizens; in particular, I've just
realized that my operator= and copy constructor are going to have to
use some sort of RTTI plus switch statements to allocate the right sort
of class to copy these things successfully -- which strikes me as
neither safe, extensible, nor prudent. In plainer language, I think
that TownOfBedrock's operator= is going to have to contain something
more or less like this:
(Assume Census is a vector<BedrockCitizen*>)
typedef std::vector<BedrockCitizen*>::iterator CitizenIt;
BedrockCitizen* aBC;
for ( CitizenIt anIt, = copyFrom.Census.begin(); anIt !=
copyFrom.Census.end(); anIt++ ) {
if ( typeid(*anIt) == typeid(Fred) ) {
aBC = new Fred;
// copy the old Fred's data into the new one.
Census.push_back(aBC);
} /* and so on for the other classes */
}
But this is an approach equally hideous and time-consuming. Can anyone
recommend a better way?
of trouble; I'd appreciate anyone's advice on the matter...
Given these classes:
class BedrockCitizen { ... };
class Fred : public BedrockCitizen { ... };
class Wilma : public BedrockCitizen { ... };
class Barney : public BedrockCitizen { ... };
..
..
..
class TownOfBedrock { ... };
What I'd like to be able to do is to create a means of storing any
number of BedrockCitizens of any of the child classes in class
TownOfBedrock, without too much inconvenience. Unfortunately, while
that can be done by having a data structure indexing
pointers-to-BedrockCitizen, and downcasting as appropriate, there's a
heck of a lot of inconvenience involved.
At the present, I'm badly bogged down in the attendant memory
management in classes using BedrockCitizens; in particular, I've just
realized that my operator= and copy constructor are going to have to
use some sort of RTTI plus switch statements to allocate the right sort
of class to copy these things successfully -- which strikes me as
neither safe, extensible, nor prudent. In plainer language, I think
that TownOfBedrock's operator= is going to have to contain something
more or less like this:
(Assume Census is a vector<BedrockCitizen*>)
typedef std::vector<BedrockCitizen*>::iterator CitizenIt;
BedrockCitizen* aBC;
for ( CitizenIt anIt, = copyFrom.Census.begin(); anIt !=
copyFrom.Census.end(); anIt++ ) {
if ( typeid(*anIt) == typeid(Fred) ) {
aBC = new Fred;
// copy the old Fred's data into the new one.
Census.push_back(aBC);
} /* and so on for the other classes */
}
But this is an approach equally hideous and time-consuming. Can anyone
recommend a better way?