Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 可接受const_内置构造函数_C++_Const Correctness - Fatal编程技术网

C++ 可接受const_内置构造函数

C++ 可接受const_内置构造函数,c++,const-correctness,C++,Const Correctness,这是const_cast的有效用法吗?我在构造函数中使用它,它如下所示: KeyLiteObject(const char * extension, const char * swap_suffix) : _extension(extension), _swap_suffix(swap_suffix), _swap_suffix_extension(swap_suffix) { const_cast<std

这是const_cast的有效用法吗?我在构造函数中使用它,它如下所示:

KeyLiteObject(const char * extension, const char * swap_suffix)
        :    _extension(extension),
             _swap_suffix(swap_suffix),
             _swap_suffix_extension(swap_suffix) 
{
    const_cast<std::string*>(&_swap_suffix_extension)->append(_extension);
}

是的,字符串永远不会更改。

假设_swap_suffix_扩展名是const std::string,那么为什么不这样做:

KeyLiteObject(const char * extension, const char * swap_suffix)
        :    _extension(extension),
             _swap_suffix(swap_suffix),
             _swap_suffix_extension( std::string( swap_suffix ) + std::string( extension ) ) 
{
}

那么您就可以完全避免const_cast…

假设_swap_suffix_扩展名是const std::string,那么为什么不这样做呢:

KeyLiteObject(const char * extension, const char * swap_suffix)
        :    _extension(extension),
             _swap_suffix(swap_suffix),
             _swap_suffix_extension( std::string( swap_suffix ) + std::string( extension ) ) 
{
}

然后,您可以完全避免常量转换…

只要有可能,请避免常量转换,在这里,对于任何给定类型都是完全可能的。只需创建一个helper函数,该函数接受两个参数,构成常量的最终值,并在初始值设定项中使用它:

// header file
struct test { 
   const type x; 
   test( type const & a, type const & b );
};
// implementation file
namespace {
   type compose( type const & arg1, type const & arg2 ) {
      // calculate the actual value here
   }
}
test::test(type const & a, type const & b) 
      : x( compose(a,b) ) 
{} 
这样做的代价是只需在实现文件中写入一个空闲静态函数或一个未命名的命名空间函数,如果为函数选择一个合适的名称,那么结果是可读的。在您的情况下:concatenate或concat将是不错的选择


虽然在示例中使用const_cast不会导致未定义的行为,但出于个人原因,我会避免使用它。在C++中,与C或java相比,强制转换是繁琐的,这是因为它们会引起程序员的注意:这里有些奇怪的事情发生了!如果你开始浇铸,那么你就会习惯于看到它们,它们就会变得自然。

只要可能,就要避免const_cast,在这里,任何给定类型的浇铸都是很可能的。只需创建一个helper函数,该函数接受两个参数,构成常量的最终值,并在初始值设定项中使用它:

// header file
struct test { 
   const type x; 
   test( type const & a, type const & b );
};
// implementation file
namespace {
   type compose( type const & arg1, type const & arg2 ) {
      // calculate the actual value here
   }
}
test::test(type const & a, type const & b) 
      : x( compose(a,b) ) 
{} 
这样做的代价是只需在实现文件中写入一个空闲静态函数或一个未命名的命名空间函数,如果为函数选择一个合适的名称,那么结果是可读的。在您的情况下:concatenate或concat将是不错的选择


虽然在示例中使用const_cast不会导致未定义的行为,但出于个人原因,我会避免使用它。在C++中,与C或java相比,强制转换是繁琐的,这是因为它们会引起程序员的注意:这里有些奇怪的事情发生了!如果你开始喷洒石膏,那么你就会习惯于看到它们,它们就会变得自然。

只是为了记录在案,可能会让初学者更困惑,但要更简洁,你可以让字符串连接的一侧成为std::string对象:_swap\u suffix\u extensionstd::stringswap\u suffix+\u extension。如果我没有一个像string这样定义得很好的对象,那么在构造函数体中使用const\u cast会被接受吗?@Tony:是的,我知道,我差点忘了。我只是觉得上面的情况更明显一些。我想它可能会慢一些,但这取决于std::string::operator+std::string,char*是否在使用普通std::string之前构建std::string对象,std::string add or not…顺便说一句/另一种方法是将swap_后缀和/或扩展参数设置为const std::string&-这样不仅可以传递char*值并隐式构造字符串,但是你可以在没有任何非直观代码的情况下连接它们。@Hassan:在我看来,人们可能不同意你不应该使用const_cast。如果您需要使用它,那么您的设计中就有一个缺陷。事实上,它只是用来处理库没有以正确的常量编写的情况。这样,你就可以继续生活,而不必提出请求来修复库代码,如果库代码确实得到了积极支持的话。作为记录,这可能会让初学者感到困惑,但要简洁一些,你可以让字符串连接的一侧成为std::string对象:_swap\u suffix\u extensionstd::stringswap\u suffix+\u extension。如果我没有一个像string这样定义得很好的对象,那么在构造函数体中使用const\u cast会被接受吗?@Tony:是的,我知道,我差点忘了。我只是觉得上面的情况更明显一些。我想它可能会慢一些,但这取决于std::string::operator+std::string,char*是否在使用普通std::string之前构建std::string对象,std::string add or not…顺便说一句/另一种方法是将swap_后缀和/或扩展参数设置为const std::string&-这样不仅可以传递char*值并隐式构造字符串,但是你可以在没有任何非直观代码的情况下连接它们。@Hassan:在我看来,人们可能不同意你不应该使用const_cast。如果您需要使用它,那么您的设计中就有一个缺陷。事实上,它只是用来处理库没有以正确的常量编写的情况。这样,如果库代码确实得到了积极的支持,您实际上就可以继续生活,而不必提出请求来修复库代码。