PDA

View Full Version : object instantiation



drkbkr
28th June 2006, 16:46
Hello all,

I come from a Java background where you can, given just a String that represents a class name, create an instance of that class. Something like:



Class c = Class.forName("com.foo.MyClass");
MyObject o = c.newInstance();


Is there a similar mechanism provided by Qt? Or C++ in general?

Any help is greatly appreciated.

Thanks,
Derek

high_flyer
28th June 2006, 17:11
/* Provided the constructor does not take any parameters*/
SomeClass *c = new SomeClass();

GreyGeek
28th June 2006, 17:19
Given that strHTMLtext was populated with HTML code earlier:


QFont f("TypeWriter",10);
QTextEdit *editor = new QTextEdit(this);
editor->setHtml(strHTMLtext);
editor->setCurrentFont(f);
QTextDocument *document = editor->document();
QPrinter printer;
this->setCursor(Qt::ArrowCursor);
QApplication::processEvents();
QPrintDialog *dlg = new QPrintDialog(&printer,this);
if (dlg->exec() == QDialog::Accepted)
document->print(&printer);
ui.leStatus->setText("Report completed!");
delete dlg;
delete editor;

drkbkr
28th June 2006, 17:25
Let me try to state the problem again.

At compile time, I don't know anything except that I will want to create an instance of some class, but I don't know which class.

At run time, I have nothing but a string that represents the name of that class.

Is there a way to create an instance of that class given only the string?

Thanks,
Derek

high_flyer
28th June 2006, 18:30
For an arbitrary class names I am not sure it is possible in C/C++.
However, I can think of a way to do it in a given Application where you know all the classes you have.
You can then make a method that will take the string and parse it with a switch case statement, and initiate the class that corresponds to the string given to it.

EDIT: since we are talking about strings, you will have to use if() statments, not a case switch.
Like:



void * Initiater::newClass(QString className)
{
if(className == "ClassA") return new ClassA();
if(className == "ClassB") return new ClassB();
//etc...
}

Big Duck
28th June 2006, 18:32
Yes, in C++ this is possible. Probably a factory method design pattern:

Try this : http://gsraj.tripod.com/design/creational/factory/factory.html

As for C++ code or Qt code, try Bruce Ekcel's 'Thinking in C++ vol 2'

A free online e-book. Chapter 10: Design patterns - Factories
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

high_flyer
28th June 2006, 18:49
Yes, in C++ this is possible. Probably a factory method design pattern:
As far as I understand this is not meant to deal with the problem that drkbkr has,rather, its to be used when you have an abstract classes, and it is unknown which classes should be initialized by the implementations of these abstract classes, as it says in the link you gave:

"Obviously, a factory is not needed to make an object. A simple call to new will do it for you. However, the use of factories gives the programmer the opportunity to abstract the specific attributes of an Object into specific subclasses which create them."

But that is only how I understood it, I could be wrong.

rickbsgu
29th June 2006, 03:26
Let me try to state the problem again.

At compile time, I don't know anything except that I will want to create an instance of some class, but I don't know which class.

At run time, I have nothing but a string that represents the name of that class.

Is there a way to create an instance of that class given only the string?

Thanks,
Derek
Your example is a bit of reflection magic that Java can do because it knows everything about the named class at runtime.

C++ is a bit stupider in that regard - so the short answer is, no, there is nothing built into the language that will do what you want. You'll have to code something like that, yourself. The factory class suggestion is a common approach for this kind of thing.

Remember in C++ you are entirely responsible for memory management and class instantiation/destruction. Furthermore, knowledge stops at the base-class/derived-class boundary. To create a derived class, something at some point has to know all the details (header include) of that derived class and either call a 'new' on it, or return a reference to an already-created instance.

That said, there are a lot of tricks and design patterns to do that. I'd start with the factory pattern.

rickb