Re: initializing a class using upcasted object
mati <longraider@gazeta.NOSPAM.pl> wrote in message...
Hi Here is the code:
class Base{ public:
Base(){...}
virtual ~Base(){}
virtual void foo() const =0;
};
class Der1 : public Base { public:
Der():Base() {...}
~Der() {...}
void foo() const {...}
};
There are several Der.. classes, and the problem is that I want to make
some X class, that will have a handle to the Base, in order to use
polymorphism.
Initialization of X objects will determine which actual object in the
Base's hierarchy will be used (Der1 or Der2 or ..), but I have no idea
how to do that in "nice" way.
<snip>
Any ideas on how it _should_ be done?
Thanks in advance.
Not sure what you are trying to do. Maybe the 'Command pattern'
(modified for downcast) will give you an idea.
#include <iostream>
#include <vector>
// ---------------------
class Command{ public:
virtual ~Command(){}
virtual void execute(std::ostream&)const = 0;
};
// ---------------------
class HelloWorld : public Command { public:
void execute(std::ostream& eout)const{
eout << "Hello World! ";
}
void ExtraFunc(std::ostream& eout){eout<<std::endl;}
};
// ---------------------
class IAm : public Command { public:
void execute(std::ostream& eout)const{
eout << "I'm the command pattern!";
}
};
// ---------------------
// - Object that holds Commands: -
class RunCom{
std::vector<Command*> commands;
std::ostream &out;
public:
RunCom(std::ostream &rcout):out(rcout){}
// ---------------------
void Add(Command &c){ commands.push_back(&c); }
void Add(Command *c){ commands.push_back(c); } // for 'new'
// ---------------------
void Run(){
std::vector<Command*>::iterator it( commands.begin() );
while( it != commands.end() ){
// (*it++)->execute( out );
(*it)->execute(out);
// - you need to cast -
HelloWorld *hw = dynamic_cast<HelloWorld*>( *it );
if( hw ){ hw->ExtraFunc( out );}
// else{ out<<" not cast "<<std::endl;}
++it;
} // while(it)
} file://Run()
// ------------------------------------
void RunDel(){ // for 'new'
std::vector<Command*>::iterator it( commands.begin() );
while( it != commands.end() ){
(*it)->execute( out );
// - you need to cast -
HelloWorld *hw = dynamic_cast<HelloWorld*>( *it );
if( hw ){ hw->ExtraFunc( out );}
delete *it; *it = 0;
*it++;
} file://while(it)
} file://RunDel()
// ------------------------------------
}; file://class RunCom
// ---------------------
int main(){
{
RunCom Rc( std::cout );
HelloWorld HellW;
Rc.Add( HellW );
IAm Popeye;
Rc.Add( Popeye );
Rc.Run();
}
RunCom Rc2( std::cout );
Rc2.Add( new HelloWorld );
Rc2.Add( new IAm );
Rc2.RunDel();
return 0;
} // main()
If you can't adopt that, you may need something along the line of
'[Abstract] Factory pattern'.
--
Bob R
POVrookie