Results 1 to 8 of 8

Thread: object instantiation

  1. #1
    Join Date
    Jan 2006
    Posts
    39
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default object instantiation

    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:

    Qt Code:
    1. Class c = Class.forName("com.foo.MyClass");
    2. MyObject o = c.newInstance();
    To copy to clipboard, switch view to plain text mode 

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

    Any help is greatly appreciated.

    Thanks,
    Derek

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: object instantiation

    Qt Code:
    1. /* Provided the constructor does not take any parameters*/
    2. SomeClass *c = new SomeClass();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: object instantiation

    Given that strHTMLtext was populated with HTML code earlier:
    Qt Code:
    1. QFont f("TypeWriter",10);
    2. QTextEdit *editor = new QTextEdit(this);
    3. editor->setHtml(strHTMLtext);
    4. editor->setCurrentFont(f);
    5. QTextDocument *document = editor->document();
    6. QPrinter printer;
    7. this->setCursor(Qt::ArrowCursor);
    8. QApplication::processEvents();
    9. QPrintDialog *dlg = new QPrintDialog(&printer,this);
    10. if (dlg->exec() == QDialog::Accepted)
    11. document->print(&printer);
    12. ui.leStatus->setText("Report completed!");
    13. delete dlg;
    14. delete editor;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Posts
    39
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: object instantiation

    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

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: object instantiation

    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:
    Qt Code:
    1. void * Initiater::newClass(QString className)
    2. {
    3. if(className == "ClassA") return new ClassA();
    4. if(className == "ClassB") return new ClassB();
    5. //etc...
    6. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    May 2006
    Posts
    28
    Thanks
    8
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: object instantiation

    Yes, in C++ this is possible. Probably a factory method design pattern:

    Try this : http://gsraj.tripod.com/design/creat...y/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/...ngInCPP2e.html

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: object instantiation

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Earth (Terra)
    Posts
    87
    Thanks
    4
    Thanked 6 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: object instantiation

    Quote Originally Posted by drkbkr
    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

Similar Threads

  1. Replies: 4
    Last Post: 10th May 2006, 22:37
  2. Application very heavy for lots object
    By Kapil in forum Newbie
    Replies: 1
    Last Post: 29th April 2006, 17:33
  3. Passing Object to dll
    By ankurjain in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 09:50
  4. Signal-Slot, object instances
    By ct in forum Qt Programming
    Replies: 3
    Last Post: 16th February 2006, 19:08
  5. Using QSA: A very basic question
    By yogeshm02 in forum Newbie
    Replies: 3
    Last Post: 26th January 2006, 07:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.