Dear friends:
I write a vector class "vec" based on the template, i am confused greatly by the "const" keyword appeared at the left most when the = operator is defined.

const vec& operator=(const vec & a)

Could you please tell me what is the function of this "const". I known that when the member function operates on some const variables, it should be declared as const also, but the const keyword is placed at the end behind the function declaration. When i move this const to the end, the codeblock gives me the following error:

f:\cblockmain\obj\vector.h|34|error C2440: 'return' : cannot convert from 'const vec<T,N>' to 'vec<T,N> &'|
f:\cblockmain\obj\vector.h|36|error C2166: l-value specifies const object|
f:\cblockmain\obj\vector.h|34|error C2440: 'return' : cannot convert from 'const vec<T,N>' to 'vec<T,N> &'|
f:\cblockmain\obj\vector.h|37|error C2440: 'return' : cannot convert from 'const vec<T,N>' to 'vec<T,N> &'|
||=== Build finished: 3 errors, 0 warnings ===|

The header file are as follows:

#ifndef VECTOR_H_INCLUDED
#define VECTOR_H_INCLUDED

#include<iostream>
using namespace std;

template<class T, int N> class vec
{
    private:
    T component[N];
    public:
    vec(){}

    vec( const T& a)
    {
         for(int i=0; i<N; i++) component[i]=a;
    }

    vec( const T& a, int n, char*)
    {
    for(int i=0; i<N; i++) component[i]=0;

    component[n]=a;
}

    vec(const vec<T,N>& v)
    {
        for(int i=0; i<N; i++) component[i]=v.component[i];
    }

   const vec& operator=(const vec & a)
    {
       if(this==&a)
       return *this;

        for(int i=0; i<N; i++) component[i]=a.component[i];
        return *this;
    }


   const vec& operator=(const T & a)
   {
   for(int i=0; i<N; i++) component[i]=a;
   return *this;
   }

    ~vec()
    {

    }


   T& operator[] (int i) const
    {
    return component[i];
    }

    void show()
    {
        for(int i=0; i<N; i++)
        {
            cout<<component[i]<<endl;
         }
    }
};

#endif // VECTOR_H_INCLUDED

This is the main file:

#include <iostream>
#include "obj/vector.h"
using namespace std;

int main()
{

        vec<double,4> V(1.0,3,"l love you");
         V.show();

        vec<double,4> V2(V);

          vec<double,4> V3;


       V3=V2;


        V3.show();



    return 0;
}

google c++ const

When the const is at the leftmost place, it is attached to the return value. In that = operator, it means that the function returns a const reference to itself.

When the const is at the rightmost place (after the parentheses), it is attached to the object to which to member function is called on (i.e. the this pointer). This means that the object and its data members cannot be modified in the body of the function.

The error you get is from the fact that when you have a const object, you can only call const member functions on it (with the const at the end), to guarantee that it will not be modified.

For the = operator, you cannot put it as const (at the end) because, by definition, the assignment operator will modify the data members within the function (i.e. to assign new values to them). Similarly, your [] operator is wrong. If you declare it const (at the end), you cannot return a non-const reference to one of its elements, so typically, it is implemented as follows:

const T& operator[] (int i) const   //takes a const "this" and returns a const ref.
    {
      return component[i];
    }
    T& operator[] (int i)      //takes a non-const "this" and return a non-const ref. 
    {
      return component[i];
    };

The difference between returning a const reference and a non-const reference in the case of the assignment operator is that if you return a non-const reference, the following would be possible:

vec<T> v1,v2,v3;
(v1 = v2) = v3;   //this will assign v2 to v1, and then assign v3 to v1 (because the first assignment return a non-const reference, to it can still be modified.

Sometimes, the above is useful, but usually not, it usually it counter-intuitive and should be avoided.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.