1

Topic: const + clever pointers

I want to make constant object the pointer on which will be stored in the clever pointer. But there is a question on that how to make object constant:

const shared_ptr <VariativeData> Settings = std:: make_shared <VariativeData> ();
...
Settings-> Add ("gfdgfd", 1135);//THERE IS NO ERROR!!!

How to forbid to edit the object which is under shared_ptr?

2

Re: const + clever pointers

All thanks, seem understood. It is necessary to specify in template type a qualifier const:

shared_ptr <const VariativeData> Settings;

And const before the clever pointer does not allow to redirect it on other object.

3

Re: const + clever pointers

AlekseySQL wrote:

And const before the clever pointer does not allow to redirect it on other object.

Allows

4

Re: const + clever pointers

Anatoly Moskovsky wrote:

it is passed...
Allows

And, if meant that it is impossible to change the pointer - that yes.

5

Re: const + clever pointers

Anatoly Moskovsky wrote:

it is passed...
And, if meant that it is impossible to change the pointer - that yes.

Wrote such a miracle - the code:

using sptr_VariativeData = std:: shared_ptr <const VariativeData>;
void SetGlobalVariable (sptr_VariativeData CurrentSettings) noexcept;
void Create ()
{
const sptr_VariativeData Settings = CreateSettings ();
SetGlobalVariable (Settings);
}

Perfectly gathers! Why, after all my variable is declared as const, and accepting function does not guarantee const???

6

Re: const + clever pointers

Pancake, well I and the sucker!
I transferred on value smile
I ask not to throw in me  rags, I subconsciously thought that it is the pointer...

7

Re: const + clever pointers

AlekseySQL wrote:

it is passed...
Wrote such a miracle - the code:

using sptr_VariativeData = std:: shared_ptr <const VariativeData>;
void SetGlobalVariable (sptr_VariativeData CurrentSettings) noexcept;
void Create ()
{
const sptr_VariativeData Settings = CreateSettings ();
SetGlobalVariable (Settings);
}

Perfectly gathers! Why, after all my variable is declared as const, and accepting function does not guarantee const???

std:: shared_ptr <const VariativeData>;

Has not enough sense. You delete it cannot...

8

Re: const + clever pointers

AlekseySQL wrote:

the Pancake, well I and the sucker!
I transferred on value smile
I ask not to throw in me  rags, I subconsciously thought that it is the pointer...

It is the pointer, but you transferred it on value.
smile

9

Re: const + clever pointers

AlekseySQL wrote:

I want to make constant object the pointer on which will be stored in the clever pointer. But there is a question on that how to make object constant:

const shared_ptr <VariativeData> Settings = std:: make_shared <VariativeData> ();
...
Settings-> Add ("gfdgfd", 1135);//THERE IS NO ERROR!!!

How to forbid to edit the object which is under shared_ptr?

It has not enough sense, you cannot delete such object.
And an essence of the clever pointer just in that object to delete when it will be not already necessary.

10

Re: const + clever pointers

MasterZiv wrote:

you cannot delete such object.

What? It is possible more in detail?

11

Re: const + clever pointers

MasterZiv wrote:

std:: shared_ptr <const VariativeData>;

Has not enough sense. You delete it cannot...

Really if I do the object which is under shared_ptr, constant then in the absence of links on shared_ptr it will not be deleted??? Whereas to forbid to change objects under shared_ptr?

12

Re: const + clever pointers

AlekseySQL wrote:

it is passed...
Really if I do the object which is under shared_ptr, constant then in the absence of links on shared_ptr it will not be deleted??? Whereas to forbid to change objects under shared_ptr?

Yes all will be deleted, is simple MasterZiv did not sleep))