Results 1 to 8 of 8

Thread: Some weird constructors

  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.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Some weird constructors

    1 - that is undefined behaviour and just happens to work for you. for someone else it may crash.

    2 - that is doing exactly the same as 1, believe it or not.
    "QString object gets created just by using class name with parantheses?"
    Yes, that is correct.

    "It is not even creates an object."
    Wrong, yes it does.

    This is very basic c++. QString("gfds") is the QString ctor that takes a const char* (string literal) !!


    "Turning back to original subject, How an object gets created just by using it's[sic] class name?"
    Welcome to c++.


    p.s.
    Qt Code:
    1. QString obj = new QString("Hi there"); // This creates object on the heap. << wrong, this wont even compile
    2. // you mean
    3. // QString* obj = new QString("Hi there");
    4. QString obj("Hello There"); // Likewise This one creates an object on the stack.
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Dec 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Some weird constructors

    Thank you very much for your reply. After I read your post I dug a lot about subject and gathered following results.

    Regarding your first answer:
    1 - that is undefined behaviour and just happens to work for you. for someone else it may crash.
    Actually, the reason it works is because of automatic type conversion made by compiler. const char* and const QString& are totaly different types. But when someone tries to pass a char* to QString& parameter, compiler behind the scenes tries to conver char* into a QString if it can. In order to do that it needs one thing, a QString constructor which takes a char* parameter. We all know that QString class has such a overloaded constructor. For that reason automatic type conversion successfully happens and char* gets converted into a QString.

    I think the same thing happens below. I mean it should be automatic type convertion too. What do you think?
    Qt Code:
    1. QString str = "Hello World";
    To copy to clipboard, switch view to plain text mode 

    Regarding your second answer:
    "It is not even creates an object."
    Wrong, yes it does.
    Your answer is correct. QString("Blah blah") alone can create an object. In literature it is called anonymous object. It is such an object that it hasn't any variable assigned to.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Some weird constructors

    Quote Originally Posted by highnergy View Post
    Actually, the reason it works is because of automatic type conversion made by compiler.
    You should be getting a warning here as you are passing a temporary object as a non-const reference. Are you sure the function prototype is what you posted and not "const QString&"?

    QString("Blah blah") alone can create an object.
    Yeah, it's called a "constructor"
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Some weird constructors

    Quote Originally Posted by highnergy View Post
    Thank you very much for your reply. After I read your post I dug a lot about subject and gathered following results.

    Regarding your first answer:

    Actually, the reason it works is because of automatic type conversion made by compiler. const char* and const QString& are totaly different types. But when someone tries to pass a char* to QString& parameter, compiler behind the scenes tries to conver char* into a QString if it can. In order to do that it needs one thing, a QString constructor which takes a char* parameter. We all know that QString class has such a overloaded constructor. For that reason automatic type conversion successfully happens and char* gets converted into a QString.

    I think the same thing happens below. I mean it should be automatic type convertion too. What do you think?
    Qt Code:
    1. QString str = "Hello World";
    To copy to clipboard, switch view to plain text mode 

    Regarding your second answer:

    Your answer is correct. QString("Blah blah") alone can create an object. In literature it is called anonymous object. It is such an object that it hasn't any variable assigned to.
    first part - I already know about what you call type conversion. I already took that into consideration. The reason why I specified undefined behaviour is because you are passing an rvalue into a reference type. The reference lasts longer than the thing it is meant to be a reference of - this is undefined behaviour.

    second part - more technically it is called an rvalue.

    Quote Originally Posted by wysota View Post
    You should be getting a warning here as you are passing a temporary object as a non-const reference. Are you sure the function prototype is what you posted and not "const QString&"?
    even if it's a const ref it's not valid c++ 2003 is it?. Not sure about c++11
    Last edited by amleto; 22nd December 2012 at 23:38.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Some weird constructors

    Quote Originally Posted by amleto View Post
    even if it's a const ref it's not valid c++ 2003 is it?
    It is valid. That's a special case all standard compilers handle properly. I think a non-const reference to a temporary should be handled safely too however the compiler should spit out a warning about it.

    Not sure about c++11
    Also valid in C++11 however sometimes it can be optimized more (by using move constructors and move assignment operators).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Some weird constructors

    did some digging and I came across this:
    The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference except as specified below. A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits. A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  8. #8
    Join Date
    Dec 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Some weird constructors

    @amleto, @wysota:

    Wow, thank you very much for such a detailed explaination in a very short time.

    best regards

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.