lucaboni
lucaboni

Reputation: 2424

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?

Upvotes: 3

Views: 2523

Answers (4)

Nikita
Nikita

Reputation: 6427

shared_ptr<const Foo> ptr is similar to const Foo * ptr. So it's a pointer to const Foo.

Upvotes: 3

Krzysztof Bargieł
Krzysztof Bargieł

Reputation: 143

std::shared_ptr<const T> allows you just to read from pointed address in memory.

Upvotes: 2

Arnav Borborah
Arnav Borborah

Reputation: 11787

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

Upvotes: 5

molbdnilo
molbdnilo

Reputation: 66451

It creates a shared const Foo - that is, const has its regular meaning.

Upvotes: 2

Related Questions