Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding C++ make shared pointer with const arguments

I found somewhere this piece of code:

boost::shared_ptr<const Foo> pFoo = boost::make_shared<const Foo>();

What's the aim of the const keyword here?

like image 955
lucaboni Avatar asked Dec 21 '25 01:12

lucaboni


1 Answers

Its very simple, it really is just a pointer pointing to a const Foo. The code, currently, which is:

boost::shared_ptr<const Foo> pFoo = boost::make_shared<const Foo>();

Is the basic equivalent of

const Foo * pFoo

The meaning of const here is regular as with const pointers The advantage of this is that the pointer is read-only, because of constness

like image 135
Arnav Borborah Avatar answered Dec 23 '25 14:12

Arnav Borborah