c++ - 为 const 输入参数传递非常量对象?

标签 c++ parameters constants parameter-passing input-parameters

目前我正在分析以下代码片段:

fvc::surfaceSum(mag(phi))().internalField() 

template<class Type>
tmp<GeometricField<Type, fvPatchField, volMesh> > surfaceSum
(
    const tmp<GeometricField<Type, fvsPatchField, surfaceMesh> >& tssf
)
{
    tmp<GeometricField<Type, fvPatchField, volMesh> > tvf = surfaceSum(tssf());
    tssf.clear();  //If object pointer points to valid object:delete object and 
                           //set pointer to NULL
    return tvf;
}

template<class Type, template<class> class PatchField, class GeoMesh>
tmp<GeometricField<scalar, PatchField, GeoMesh> > mag
(
    const GeometricField<Type, PatchField, GeoMesh>& gf
)
{
    tmp<GeometricField<scalar, PatchField, GeoMesh> > tMag
    (
          new GeometricField<scalar, PatchField, GeoMesh>
          (
                IOobject
                (
                      "mag(" + gf.name() + ')',
                      gf.instance(),
                      gf.db(),
                      IOobject::NO_READ,
                      IOobject::NO_WRITE
                ),
                gf.mesh(),
                gf.dimensions()
          )
    );

    mag(tMag(), gf);

    return tMag;
}

template<class T>
inline const T& Foam::tmp<T>::operator()() const       
{
    if (isTmp_) // bool isTmp_//Flag for whether object is a temporary or a constant object
    {
          if (!ptr_)   //mutable T* ptr_;  //- Pointer to temporary object   
          {
                FatalErrorIn("const T& Foam::tmp<T>::operator()() const")
                << "temporary deallocated"
                << abort(FatalError);
          }

          return *ptr_;  
    }
    else
    {
          return ref_; //const T& ref_  //- Const reference to constant object
    }
}

template<class Type, template<class> class PatchField, class GeoMesh>
typename
Foam::GeometricField<Type, PatchField, GeoMesh>::InternalField 
{                                                                                                                         
    this->setUpToDate();  //Set up to date                                                   
    storeOldTimes();    //Store the old-time fields.
    return *this;
}

第二个代码片段中的方法 surfaceSum(...) 需要 const tmp<GeometricField<Type, fvsPatchField, surfaceMesh> >&作为输入参数,但是当通过其他方法时,我得到的结果是一个非常量参数 tmp<GeometricField<scalar, PatchField, GeoMesh> > tMag (参见第三个代码片段)。因此,是否可以为 const 传递一个非常量对象 输入参数还是我在这里误解了什么?

问候 直

最佳答案

我找不到任何对 surfaceSum 的调用(一些 typedef 会大大提高可读性),但是,是的,您可以将非常量对象作为常量输入参数。输入中的 const 仅表示“我不会修改您传递的对象”。

关于c++ - 为 const 输入参数传递非常量对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20357904/

相关文章:

c++ - PJSIP 示例代码 app_perror 未在范围内声明

c++ - 数据库。错误 C2352 : illegal call of non-static member function

ios - 带参数调用函数

c++ - const 引用对象上引用的变异值

ruby - 如何在模块定义之外访问 Module.nesting 的返回

c++ - 为什么编译不会失败?

java - Java中函数作为参数

java - JSP 属性和参数之间的区别

compiler-errors - 如何保证const变量不会发生溢出?

c++ - 为什么静态库包含一个主要功能?