Amod wrote in message
I want to call a child class constructor using the base class instance
.. whats the xact mechanism to perform this function ?
Regards,
Amod
Sounds a little like you are looking for the 'command pattern'.
#include <iostream>
#include <ostream>
class Command{ // base class, pure virtual method
public:
virtual void Execute(std:
stream&) = 0;
};
// ------------------------------------
class HelloWorld : public Command {
public:
void Execute(std:
stream& out){ out << "Hello World! "; }
};
// ------------------------------------
class ComRun{
std::vector<Command*> commands;
public:
void add(Command *c){ commands.push_back(c); }
// ------------------------------------
void runLoc(std:
stream& Cout){
std::vector<Command*>::iterator it = commands.begin();
while(it != commands.end())
(*it++)->Execute(Cout);
} //runLoc()
// ------------------------------------
void runNew(std:
stream& Cout){
std::vector<Command*>::iterator it = commands.begin();
while(it != commands.end()){
(*it)->Execute(Cout);
delete *it; *it = 0;
*it++;
} //while(it)
} //runNew()
// ------------------------------------
}; //class ComRun
// ------------------------------------
int main(){
ComRun macro;
HelloWorld Hw;
macro.add(&Hw);
// -- add more here --
macro.runLoc(std::cout);
// macro.add(new HelloWorld);
// macro.runNew(std::cout);
return 0;
}
Is that anything like what you wanted?
[ corrections are always welcome]