Results 1 to 8 of 8

Thread: Some weird constructors

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Some weird constructors

    Hi,

    I have came across with following constructs a lot. But I couldn't give them a meaning. Please help me understand them.


    1. Qt Code:
      1. void afunction (QString& str) {...};
      To copy to clipboard, switch view to plain text mode 
      Suppose we have function and and gets a reference to a QString parameter. In order to call that function one has to create a QString object and pass it to the function.
      Qt Code:
      1. QString obj("Some string");
      2. afunction(obj);
      To copy to clipboard, switch view to plain text mode 
      But the weird thing is you don't have to create an object. You can just pass a string literal like "This is a Literal" to the function instead of a QString object;
      Qt Code:
      1. afunction("This is a Literal");
      To copy to clipboard, switch view to plain text mode 
      and it works. Why?

    2. Apart from sending a string literal to the above function there is also another weird way to call the function.
      Qt Code:
      1. afunction(QString("Hello there"));
      To copy to clipboard, switch view to plain text mode 
      Above call works but it shouldn't. How an QString object gets created just by using class name with parantheses? It is not even creates an object. What is the meaning of QString("Hi there") alone in the function parameter list? First of all it is not the way books thought us creating objects. I can create a QString object with two ways that I know.
      Qt Code:
      1. QString obj = new QString("Hi there"); // This creates object on the heap.
      2. QString obj("Hello There"); // Likewise This one creates an object on the stack.
      To copy to clipboard, switch view to plain text mode 
      I don't know any other way to create an object. Turning back to original subject, How an object gets created just by using it's class name?


    regards
    Last edited by highnergy; 22nd December 2012 at 13:34.

Similar Threads

  1. weird weird mingw32-make
    By KillGabio in forum Qt Programming
    Replies: 19
    Last Post: 20th January 2012, 20:09
  2. Qwt 6.0.0 and removal of copy constructors.
    By sdhengsoft in forum Qwt
    Replies: 4
    Last Post: 21st April 2011, 04:33
  3. Using template in constructors
    By estanisgeyer in forum General Programming
    Replies: 4
    Last Post: 18th November 2008, 18:41
  4. Mess with inherited constructors
    By Pepe in forum Newbie
    Replies: 8
    Last Post: 8th September 2007, 17:16
  5. constructors, new, return
    By TheKedge in forum General Programming
    Replies: 1
    Last Post: 29th September 2006, 14:43

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
  •  
Qt is a trademark of The Qt Company.