문제

I have a code base, in which for Matrix class, these two definitions are there for () operator:

template <class T> T& Matrix<T>::operator() (unsigned row, unsigned col)
{
    ......
}


template <class T> T Matrix<T>::operator() (unsigned row, unsigned col) const
{
    ......
}

One thing I understand is that the second one does not return the reference but what does const mean in the second declaration? Also which function is called when I do say mat(i,j)?

도움이 되었습니까?

해결책

Which function is called depends on whether the instance is const or not. The first version allows you to modify the instance:

 Matrix<int> matrix;
 matrix(0, 0) = 10;

The const overload allows read-only access if you have a const instance (reference) of Matrix:

 void foo(const Matrix<int>& m)
 {
     int i = m(0, 0);
     //...
     //m(1, 2) = 4; //won't compile
 }

The second one doesn't return a reference since the intention is to disallow modifying the object (you get a copy of the value and therefore can't modify the matrix instance).

Here T is supposed to be a simple numeric type which is cheap(er) to return by value. If T might also be a more complex user-defined type, it would also be common for const overloads to return a const reference:

 template <class T>
 class MyContainer
 {
      //..,
      T& operator[](size_t);
      const T& operator[](size_t) const;
 }

다른 팁

The const version will be called on const Matrices. On non-const matrices the non-const version will be called.

Matrix<int> M;
int i = M(1,2); // Calls non-const version since M is not const
M(1,2) = 7; // Calls non-const version since M is not const

const Matrix<int> MConst;
int j = MConst(1,2); // Calls const version since MConst is const

MConst(1,2) = 4; // Calls the const version since MConst is const.
                 // Probably shouldn't compile .. but might since return value is 
                 // T not const T.

int get_first( const Matrix<int> & m )
{
   return m(0,0); // Calls the const version as m is const reference
}

int set_first( Matrix<int> & m )
{
  m(0,0) = 1; // Calls the non-const version as m is not const
}

SharePoint 2013은 2010 년 (V14) 및 2013 (V15)의 두 가지 모드가 있다는 사실에 의해 여기에서 내 문제가 발생했습니다.분명히 기본적으로 새로운 SharePoint 2013 설치는 대부분 V15 기능 만 설치합니다.v14 기능을 설치하지는 않지만 필요한 것을 설치하지 못했습니다. SharePoint 기능 관리 도구 "Nofollow"> SharePoint 기능 관리 를 사용 하여이 기능을 표시했습니다.아주 편리합니다.그 지식으로 무장 한 v14 기능을 설치하기 위해 SharePoint 2013 관리 셸에서 실행 해야하는 cmdlet을 발견했습니다.각각의 누락 된 기능을 개별적으로 설치하고 V14 또는 V15 하이브에 기존 기능을 모두 설치하는 다음 cmdlet을 실행하여이를 수행했습니다.

Install-SPFeature -AllExistingFeatures
.

모든 것이 하나의 샷에 설치 될 수 있으므로 두 가지 방법이 쉽고 내 업그레이드로 앞으로 나아 갔던 접근 방식입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top