Thread: String in C++

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    String in C++

    Hi guys!
    String in C++ is just an object of class, in C we were using
    Code:
    char *
    and all fine with that also over C++ support that.
    but what's confusing me, if I'm using string data type as String , in other words using the facility that C++ provide, why while passing string arguments I must write
    Code:
     Fun(String &argument)
    and not just write
    Code:
     Fun(String argument)
    ?

    I'm really confused about that ! , I'm making an analogy to
    String <=> char * , so why I need while calling function by String value to identify the argument of the function that's type String as &argument ?

    for example:
    Code:
    void(std:: const String &argument)
    {
    cout<<argument;
    }

  2. #2
    Registered User
    Join Date
    May 2019
    Posts
    209
    C++ Syntax for $400, What is a reference?

    References - C++ Reference - Cprogramming.com

    Without the & in the function declaration, the String object is copied. When passed by reference, it isn't. Also, what the function may modify in the string is still in scope after the function returns. It is similar to passing by pointer in C, but has very different implications.

    One implication is that in all but a few contrived circumstances, the reference can't be nullptr. If a pointer were used, the pointer might be nullptr, but with references it should be possible to assume it isn't.

    Now, there are obfuscations in C++ people use to try to show how it can be made nullptr, but you have to actually work at it.
    Last edited by Niccolo; 05-27-2019 at 07:47 PM. Reason: Update

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-01-2013, 10:11 PM
  2. Replies: 1
    Last Post: 04-27-2013, 04:36 AM
  3. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  4. Replies: 7
    Last Post: 06-16-2011, 06:21 PM
  5. Replies: 1
    Last Post: 10-31-2005, 11:36 AM

Tags for this Thread