shared_ptr容器的constness及其元素的constness [英] Constness of the container of shared_ptr and the constness of it elements

查看:56
本文介绍了shared_ptr容器的constness及其元素的constness的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的程序中,我想要一个迭代器包含指针指向

到常量对象而不是const指针。如果有可能,你能告诉我怎么做吗?


#include< boost / shared_ptr.hpp>

#include< vector>

#include< iterator>

#include< iostream>


class审判{

public:

void const_fun()const {

std :: cout<< __PRETTY_FUNCTION__<< std :: endl;

}

void non_const_fun(){

std :: cout<< __PRETTY_FUNCTION__<< std :: endl;

}

};


int main(){

std ::矢量<提高:: shared_ptr的<试验> > v;

v.push_back(boost :: shared_ptr< trial>(新试用));

{

std :: vector< boost :: shared_ptr的<试验> > :: iterator it = v.begin();

(* it) - > const_fun();

(* it) - > non_const_fun() ;

}

{

std :: vector< boost :: shared_ptr< trial> > :: const_iterator it =

v.begin();

(* it) - > const_fun();

(* it) - > non_const_fun(); //想要一个const试用,这个函数应该没有被调用


}

{

std :: vector< boost :: shared_ptr< const trial> > :: iterator it =

v.begin(); //编译错误

(* it) - > const_fun();

(* it) - > non_const_fun();

}


std :: vector< boost :: shared_ptr< const trial> > v_const = v; //错误,

//这是什么转换

这个?


返回EXIT_SUCCESS;

}

In the following program, I want an iterator contain pointer pointing
to constant object not const pointer. If it is possible would you
please let me know how to do it?

#include <boost/shared_ptr.hpp>
#include <vector>
#include <iterator>
#include <iostream>

class trial {
public:
void const_fun() const {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
void non_const_fun() {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};

int main(){
std::vector<boost::shared_ptr<trial> > v;
v.push_back(boost::shared_ptr<trial>(new trial));
{
std::vector<boost::shared_ptr<trial> >::iterator it = v.begin();
(*it)->const_fun();
(*it)->non_const_fun();
}
{
std::vector<boost::shared_ptr<trial> >::const_iterator it =
v.begin();
(*it)->const_fun();
(*it)->non_const_fun();//want a const trial, this function should
not be called.
}
{
std::vector<boost::shared_ptr<const trial> >::iterator it =
v.begin();//compile error
(*it)->const_fun();
(*it)->non_const_fun();
}

std::vector<boost::shared_ptr<const trial> > v_const = v;//error,
//is there any conversion of
this kind?

return EXIT_SUCCESS;
}

推荐答案

2006年4月2日13:59:58 -0700,Pe ** *****@gmail.com"

< Pe ******* @ gmail.com>写道:
On 2 Apr 2006 13:59:58 -0700, "Pe*******@gmail.com"
<Pe*******@gmail.com> wrote:
在下面的程序中,我想要一个迭代器包含指向常量对象而不是const指针的指针。如果有可能,请告诉我怎么做?
In the following program, I want an iterator contain pointer pointing
to constant object not const pointer. If it is possible would you
please let me know how to do it?




编号标准容器仅用于值,不用于指针(不是

真实,也不是聪明)。


祝福,

Roland Pibinger



No. Standard containers are for values only, not for pointers (neither
real nor "smart").

Best wishes,
Roland Pibinger


Roland Pibinger写道:
Roland Pibinger wrote:
2006年4月2日13:59:58 -0700,Pe ******* @ gmail.com
< Pe*******@gmail.com>写道:
On 2 Apr 2006 13:59:58 -0700, "Pe*******@gmail.com"
<Pe*******@gmail.com> wrote:
在下面的程序中,我想要一个迭代器包含指向常量对象而不是const指针的指针。如果有可能,请告诉我怎么做?
In the following program, I want an iterator contain pointer pointing
to constant object not const pointer. If it is possible would you
please let me know how to do it?



编号标准容器仅供参考,不适用于指针(不是真实的)也不是智能)。


No. Standard containers are for values only, not for pointers (neither
real nor "smart").



需要详细说明吗?容器通常用于指针和

智能指针。


-

Ian Collins。


Care to elaborate? Containers are often used for both pointers and
smart pointers.

--
Ian Collins.


在文章< 11 ********************* @ z34g2000cwc.googlegroups中。 com>,

" Pe ******* @ gmail.com" < Pe的******* @ gmail.com>写道:
In article <11*********************@z34g2000cwc.googlegroups. com>,
"Pe*******@gmail.com" <Pe*******@gmail.com> wrote:
在下面的程序中,我想要一个迭代器包含指向常量对象而不是const指针的指针。如果有可能,请告诉我怎么做?


您必须将矢量包装在您自己的类中,禁止使用这样的



#include< boost /shared_ptr.hpp>
#include< vector>
#include< iterator>
#include< iostream>

课堂试用{
public:
void const_fun()const {
std :: cout<< __PRETTY_FUNCTION__<< std :: endl;
}
void non_const_fun(){
std :: cout<< __PRETTY_FUNCTION__<< std :: endl;
}
};

int main(){
std :: vector< boost :: shared_ptr< trial> > v;
v.push_back(boost :: shared_ptr< trial>(新试用));
{
std :: vector< boost :: shared_ptr< trial> > :: iterator it = v.begin();
(* it) - > const_fun();
(* it) - > non_const_fun();
}
{
std :: vector< boost :: shared_ptr< trial> > :: const_iterator it =
v.begin();
(* it) - > const_fun();
(* it) - > non_const_fun(); //想要一个const试验,这个功能应该不被调用。


应该调用该函数。 const_iterator意味着

指针值本身是const,而不是它指向的试验对象。 IE你

可以改变试用对象,但你不能用另一个

试用对象替换它。

}
{
std :: vector< boost :: shared_ptr< const trial> > :: iterator it =
v.begin(); //编译错误
(* it) - > const_fun();
(* it) - > non_const_fun() ;


std :: vector< boost :: shared_ptr< const trial> > v_const = v; //错误,
//这种转换是什么?


不,如果你使用光头指针也不会。

返回EXIT_SUCCESS;
}
In the following program, I want an iterator contain pointer pointing
to constant object not const pointer. If it is possible would you
please let me know how to do it?
You have to wrap the vector in a class of your own that prohibits such
use.
#include <boost/shared_ptr.hpp>
#include <vector>
#include <iterator>
#include <iostream>

class trial {
public:
void const_fun() const {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
void non_const_fun() {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};

int main(){
std::vector<boost::shared_ptr<trial> > v;
v.push_back(boost::shared_ptr<trial>(new trial));
{
std::vector<boost::shared_ptr<trial> >::iterator it = v.begin();
(*it)->const_fun();
(*it)->non_const_fun();
}
{
std::vector<boost::shared_ptr<trial> >::const_iterator it =
v.begin();
(*it)->const_fun();
(*it)->non_const_fun();//want a const trial, this function should
not be called.
That function should be called. The const_iterator means that the
pointer value itself is const, not the trial object it points to. IE you
can change the trial object, but you can''t replace it with a different
trial object.
}
{
std::vector<boost::shared_ptr<const trial> >::iterator it =
v.begin();//compile error
(*it)->const_fun();
(*it)->non_const_fun();
}

std::vector<boost::shared_ptr<const trial> > v_const = v;//error,
//is there any conversion of
this kind?
No, nor would there be if you were using bald pointers.
return EXIT_SUCCESS;
}




-

魔术取决于传统和信仰。它不欢迎观察,

也不会通过实验获利。另一方面,科学的经验基于
;它可以通过观察和实验进行校正。



--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.


这篇关于shared_ptr容器的constness及其元素的constness的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆