On 25 Nov 2005 07:32:09 -0800, "Amod" <amod.gadre@gma il.com> wrote:
[color=blue]
>I want to call a child class constructor using the base class instance
>.. whats the xact mechanism to perform this function ?[/color]
You do not "call" a constructor except through calling new, placement
new, or by creating the object automatically (i.e. local or stack
variable).
What do you mean by "base class instance"? If you have an existing
base class object, you cannot make a derived object out of it except
as happens automatically by constructing the derived object; IOW its
base class part is constructed before any of the derived parts.
Give us some more information as to what you are trying to do. None of
this, BTW, has to do with "upcasting" , the subject of your message.
* Amod:[color=blue]
> I want to call a child class constructor using the base class instance
> .. whats the xact mechanism to perform this function ?[/color]
There is no such thing.
There are somewhat similar things, but I hesitate to assume any specific
meaning since it seems you don't know what you're asking.
If you could clarify, then perhaps some more concrete advice could be
offered.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Amod wrote in message
<1132932729.085 324.129840@g44g 2000cwa.googleg roups.com>...[color=blue]
>I want to call a child class constructor using the base class instance
>.. whats the xact mechanism to perform this function ?
>
>Regards,
>Amod[/color]
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::os tream&) = 0;
};
// ------------------------------------
class HelloWorld : public Command {
public:
void Execute(std::os tream& out){ out << "Hello World! "; }
};
// ------------------------------------
class ComRun{
std::vector<Com mand*> commands;
public:
void add(Command *c){ commands.push_b ack(c); }
// ------------------------------------
void runLoc(std::ost ream& Cout){
std::vector<Com mand*>::iterato r it = commands.begin( );
while(it != commands.end())
(*it++)->Execute(Cout );
} //runLoc()
// ------------------------------------
void runNew(std::ost ream& Cout){
std::vector<Com mand*>::iterato r 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(st d::cout);
Comment