Upcasting ..

A

Amod

I want to call a child class constructor using the base class instance
... whats the xact mechanism to perform this function ?

Regards,
Amod
 
B

Bob Hairgrove

I want to call a child class constructor using the base class instance
.. whats the xact mechanism to perform this function ?

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.
 
A

Alf P. Steinbach

* Amod:
I want to call a child class constructor using the base class instance
.. whats the xact mechanism to perform this function ?

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.
 
B

BobR

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::eek:stream&) = 0;
};
// ------------------------------------
class HelloWorld : public Command {
public:
void Execute(std::eek:stream& out){ out << "Hello World! "; }
};
// ------------------------------------
class ComRun{
std::vector<Command*> commands;
public:
void add(Command *c){ commands.push_back(c); }
// ------------------------------------
void runLoc(std::eek:stream& Cout){
std::vector<Command*>::iterator it = commands.begin();
while(it != commands.end())
(*it++)->Execute(Cout);
} //runLoc()
// ------------------------------------
void runNew(std::eek: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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top