const functions

I have list of functions in my code and I want to add "const".. how do i know what functions are suitable to return const or to get const or to have const on the this parameter ?
1
2
3
const function (..);
function(const A ..);
function(..) const


Thank you in advance
You can know that after reading some book on C++ for beginners.
Those three functions aren't the same.
1
2
3
const Object function(Object); //Do you need the function to return a constant?
Object function(const Object); //Do you need to make a constant copy (most of the time not)?
Object Object::function(Object)const; //Do you need the object pointed to by this be constant? 
Check out "Const correctness"

if you wanted to create a function f() that accepted a std::string, plus you want to promise callers not to change the caller's std::string that gets passed to f(), you can have f() receive its std::string parameter.
1
2
3
void f1(std::string const& s); // Pass by reference-to-const
void f2(std::string const* sptr); // Pass by pointer-to-const
void f3(std::string s); // Pass by value 


In the pass by reference-to-const and pass by pointer-to-const cases, any attempts to change the caller's std::string within the f() functions would be flagged by the compiler as an error at compile-time. This check is done entirely at compile-time: there is no run-time space or speed cost for the const. In the pass by value case (f3()), the called function gets a copy of the caller's std::string. This means that f3() can change its local copy, but the copy is destroyed when f3() returns. In particular f3() cannot change the caller's std::string object.

As an opposite example, suppose you wanted to create a function g() that accepted a std::string, but you want to let callers know that g() might change the caller's std::string object. In this case you can have g() receive its std::string parameter...

1
2
void g1(std::string& s); // Pass by reference-to-non-const
void g2(std::string* sptr); // Pass by pointer-to-non-const 


The lack of const in these functions tells the compiler that they are allowed to (but are not required to) change the caller's std::string object. Thus they can pass their std::string to any of the f() functions, but only f3() (the one that receives its parameter "by value") can pass its std::string to g1() or g2(). If f1() or f2() need to call either g() function, a local copy of the std::string object must be passed to the g() function; the parameter to f1() or f2() cannot be directly passed to either g() function. E.g.,

1
2
3
4
5
6
7
8
9
void g1(std::string& s);

void f1(std::string const& s)
{
  g1(s);          // Compile-time Error since s is const

  std::string localCopy = s;
  g1(localCopy);  // OK since localCopy is not const
}


Naturally in the above case, any changes that g1() makes are made to the localCopy object that is local to f1(). In particular, no changes will be made to the const parameter that was passed by reference to f1().


Now, we start to get a cascade of const errors that will occur if we haven't implemented const correctness from the beginning:
1
2
3
4
5
6
7
class Fred
{
    int A;
public:
    void setA(int a);
    int getA();
};


If we have a const Fred, then calling setA and getA will both return errors because both functions have the potential to change A, even though we know that getA() will probably be safe. By declaring it as:
1
2
3
4
5
6
7
class Fred
{
    int A;
public:
    void setA(int a);
    int getA() const;
};

We can be garunteed that getA will not change the object and so now it can be used by a const Fred.
Last edited on
Thank you everyone:)
Topic archived. No new replies allowed.