Thread: using const

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    using const

    Hello,

    I have some code I am trying to work out.

    My reason is that the passing to g1(s) is not allowed is that the function g1 could change the value of s.

    However, g1 doesn't change the value it is only printing the value. However, as the function f1 cannot know what function g1 will do with the value it will ALWAYS complain of a compile error.

    Am I right with my thinking?

    Many thanks,

    Code:
    void g1(std::string& s)
    {
         std::cout << "S: " << s << std::endl;
    }
     
     void f1(const std::string& s)
     {
       g1(s);          // Compile-time Error since s is const
     
       std::string localCopy = s;
       g1(localCopy);  // OK since localCopy is not const
     }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    However, g1 doesn't change the value it is only printing the value. However, as the function f1 cannot know what function g1 will do with the value it will ALWAYS complain of a compile error.

    Am I right with my thinking?
    As far as I understand it, yes. Of course, "cannot" may be too strong, since this is a matter of language design.

    Another way of looking at it is that f1 promises its caller that it will not change what s references. g1 does not make such a promise, so f1 cannot pass s to g1 without risking that its own promise may be broken.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Make the function take a const string then:
    Code:
    void g1( const std::string& s )
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Another way of looking at it is that f1 promises its caller that it will not change what s references. g1 does not make such a promise, so f1 cannot pass s to g1 without risking that its own promise may be broken.
    Great explanation. Easy to understood.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM