Thread: initialize conventions

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    40

    initialize conventions

    A few more newbee learning C++ questions:

    (1) Do the below accomplish the same thing, just different conventions ?
    Code:
    const std::string& str1(ss.str());
                                        
    const std::string& str1 = ss.str();
    
    And a bit more fuzzy, seems some cases allow this
    
    Whatever_Type Var = { Pertinent_Value };
    ---------------
    (2) In the above, the string is const and str1 is a reference to it, correct ?
    ---------------

    (3) As for references aren't they always const, i.e. I can't make it reference a different var or object other than the one it was initialized to, Correct (?).

    So there would never be a need to add the "const" to a "&" reference, correct (?)

    If there were I'm guessing it might look like this ?
    Code:
     const std::string& const str1

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    (1) They both result in the same machine code, correct.

    (1b)
    With brackets you have to be careful, you can do:
    Code:
    std::string s{"Some string"}; // calls constructor just like s("Some string") would
    It's also a general syntax to create objects, which the compiler can deduce:
    Code:
    std::string make_string()
    {
        return {"Hello"}; // returns object: std::string("Hello");
        return {}; // returns empty std::string
    }
    It has the added property that it doesn't allow narrowing conversions:
    Code:
    double d = 5.0;
    int n1(d); // fine
    int n2{d}; // error, look at the message from your compiler
    Edit: the entry form snipped away the rest of my post:

    (2) Correct, str1 is a reference to const std::string, i.e. vows not to change the referenced object.

    (3) Correct, a reference – once bound – cannot be unbound/rebound to something else, so a const specifier makes no sense but it would look like in your example.

    It's worth mentioning that r-value references exist, which can bind to a temporary value (which prior only reference-to-const could) and even modify it over its lifetime:
    Code:
    std::string&& s = "Hello"; // r-value reference, notice the double &
    s += " World!";
    cout << s << endl; // Hello World!
    Last edited by -Adrian; 06-08-2016 at 04:38 PM. Reason: typo

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by R_W_B View Post
    So there would never be a need to add the "const" to a "&" reference, correct (?)
    Not exactly. In the case of references, the reference is const, but the referred object may not be. Adding const to the reference makes the referred object const.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    40
    Quote Originally Posted by -Adrian View Post
    (1) . . . . . . .(RWB edit ->this part cut out, but thank you for the detailed confirmations) . . . .

    (3) Correct, a reference – once bound – cannot be unbound/rebound to something else, so a const specifier makes no sense but it would look like in your example.

    It's worth mentioning that r-value references exist, which can bind to a temporary value (which prior only reference-to-const could) and even modify it over its lifetime:
    Code:
    std::string&& s = "Hello"; // r-value reference, notice the double &
    s += " World!";
    cout << s << endl; // Hello World!
    Quote Originally Posted by Elkvis View Post
    Not exactly. In the case of references, the reference is const, but the referred object may not be. Adding const to the reference makes the referred object const.
    Thank you both. However . . oh gawd . . I have distant nightmares of past yrs when I took a hiatus from learning programming about things done behind ctors and operators with lvalues and rvalues.

    Something akin to when a hookah-smoking operator has returned you the call, go ask Alice what the dormouse said. (lol). Uh excuse an old man digressing as all those lyrics are quite possibly before your time.

    But seriously you've answered my questions excellent and yet stretched me to the next horizon. Of the "whys" I have really no idea yet but I will read up on them again.
    Last edited by R_W_B; 06-09-2016 at 07:30 AM. Reason: clarity

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling conventions
    By Mario F. in forum Tech Board
    Replies: 16
    Last Post: 01-15-2009, 10:55 AM
  2. naming conventions?
    By Tox|k in forum C Programming
    Replies: 44
    Last Post: 05-26-2008, 08:29 AM
  3. standard naming conventions?
    By h_howee in forum C++ Programming
    Replies: 8
    Last Post: 11-10-2007, 11:53 PM
  4. calling conventions
    By Micko in forum C Programming
    Replies: 2
    Last Post: 07-18-2004, 09:13 AM
  5. C++ conventions
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 03-14-2002, 08:15 AM

Tags for this Thread