笔记--c++临时对象与const

原创 2007年09月20日 14:24:00
c++中规定临时对象只能赋给const的引用
const A& a = A()  //合法
A& a = A()            //非法
百思不得其解,不知道为什么c++会这样规定,发帖问得到如下答案:
It is likely to lead to unexpected behaviour. Imagine something first like this:
Code:

std::string & make_upper( std::string & s )
{
std::transform( s.begin(), s.end(), s.begin(), ::toupper );
return s;
}

Reasonable enough implementation. Now first suppose you tried to pass in char * instead.

Code:

char text[] = "Hello world";
make_upper( char_text );


std::string has an implicit constructor from const char * (and thus char *) so it might create a temporary which can be used for this function, but if that were allowed, the std::string makes a copy so you would only be modifying the copy and the changes would be lost.

In case you think that this should apply only to implicit constructors and not temporaries returned from functions, there are a lot of cases whereby objects are constructed through a helper function because these functions can resolve the template parameters (needed for the class) so you don't need to supply them to the function. (bind1st and make_pair are examples of such functions).

Note that if the class is a wrapper for a non-const reference or pointer, you can work around the situation by using a const reference to the buffer. For example in class to input to a vector.

Code:

template< T >
class vec_wrapper_t
{
std::vector< T > & m_v;
public:
vec_wrapper_t( std::vector< T > & v ) : m_v ( v )
{
}

void input( std::istream & is ) const
{
// implement input to the vector
}
};

template < typename T >
vec_wrapper_t< T > vec_wrapper( std::vector< T > & v )
{
return vec_wrapper_t< T >( v );
}

template < typename T >
std::istream & operator>>( std::istream & is, const vec_wrapper_t & vw )
{
vw.input( is );
return is;
}

Above illustrated
1. That you pass in vec_wrapper_t as const reference (or value) even to the overload of operator >> from istream
2. vec_wrapper is a typical function that resolves the template parameter for you and returns a temporary.
3. with the code above, if v is a vector and is is an input stream you can write is >> vec_wrapper( v ) in your code because of the fact that it's a const reference.
4. In reality vec_wrapper_t is going to have other features, (in my case it has the delimiters used for inputting). You can't get operator>> to write these because you vec_wrapper_t is temporary, i.e. anything you wrote to it would be lost and for the next vector you tried to write to, it would not be there.

reply by me:
what do you mean is in c++ there exists plenty of codes which may create temporary objects, if we do not define temporary as const, we will suffer much more buggy codes? With the definition of temporary as const, the compiler can find out these bugs ?

相关文章推荐

printf和wprintf、printf输出结束标识符、c++按值返回临时对象是否是const的实验

#ifndef _TEST_H #define _TEST_H #include #include using namespace std; int x = 5; struct s { pub...
  • gukesdo
  • gukesdo
  • 2011年11月21日 20:40
  • 1464

C++中的临时对象都是const类型

先看代码: 1 #include 2 using namespace std; 3 4 class A {}; 5 6 class B 7 { 8 public: ...

C++中的临时对象都是const类型

http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2622193.html 先看代码: 1 #include...
  • ywh147
  • ywh147
  • 2012年09月02日 23:06
  • 442

【转】c++临时对象与const

C++临时对象的生命周期 有关临时对象的生命周期有三种情况: 1)一般情况:临时性对象的被摧毁,应该是对完整表达式(full-expression)求值过程中的最后一个步骤。该完整表达式造成临...

C++中指针,引用与const,以及const中的临时量对象的探讨

刚学完C++中的指针,引用和const,以及三者之间的qu'b

【c++笔记七】教你使用"const类型的对象、成员函数"和"static类型的成员函数、变量"

通过本文我们学到了: const对象、成员函数的使用方法,static成员变量、函数的原理和应用。 最后通过单例模式的例子把我们前面所讲到的知识点稍微串起来了一下,算是复习了。...

由重建二叉树算法题到临时对象不能绑定到非 const 的左值引用

前言最近再刷剑指offer,在将一个临时变量传给一个引用的时候,出现了non-const lvalue reference to type 'vector' cannot bind to a temp...

C++ const引用、临时变量 引用参数

C++引用—临时变量、引用参数和const引用 如果实参与引用参数不匹配,C++将生成临时变量。如果引用参数是const,则编译器在下面两种情况下生成临时变量:          实参类型是正确的...

C++l临时对象

  • 2012年12月17日 01:37
  • 123KB
  • 下载

函数返回的临时对象为const类型,const引用

#include #include     using namespace std; class A { public: A(){ m_Int = 10; } publi...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:笔记--c++临时对象与const
举报原因:
原因补充:

(最多只允许输入30个字)