Smart pointers(shared_ptr)

Hi guys, i try to learn how to use smart pointers. I use for the long time normal pointers and i think i need some upgrade of my skills.

I make some research, i understand some aspects of smart pointers, but i try to implement in a clear project to see how smart pointers work. I try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <array>
#include <memory>

class Entity
{
public:
	Entity()
	{
		std::cout << "Entity called!" << std::endl;
	}
	~Entity()
	{
		std::cout << "Entity destroyed!" << std::endl;
	}
	void print() { std::cout << "Message!"; }
};

int main()
{
	std::shared_ptr<int>f1(new int[100]);

	f1.get()[1] = 1;
	std::cout << f1.get()[1];
}


all good, message it's print. But when i try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <array>
#include <memory>

class Entity
{
public:
	Entity()
	{
		std::cout << "Entity called!" << std::endl;
	}
	~Entity()
	{
		std::cout << "Entity destroyed!" << std::endl;
	}
	void print() { std::cout << "Message!"; };
};

int main()
{
	std::shared_ptr<Entity>f1(new Entity[100]);

	f1.get()[1].print();
}


I get this error:
[img]https://i.imgur.com/U30gTgC.png[/img]


next:

1
2
3
4
5
6
int main()
{
	std::shared_ptr<Entity>f1(new Entity[100]);

	(f1.get() + 1)->print();
}


same error.


I try to use std::make_shared:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <array>
#include <memory>

class Entity
{
public:
	Entity()
	{
		std::cout << "Entity called!" << std::endl;
	}
	~Entity()
	{
		std::cout << "Entity destroyed!" << std::endl;
	}
	void print() { std::cout << "Message!"; };
};

int main()
{
	std::shared_ptr<Entity>f1 = std::make_shared<Entity>();

	f1->print();
}


everything it's ok.

I try to alocate continue memory with int:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <array>
#include <memory>

class Entity
{
public:
	Entity()
	{
		std::cout << "Entity called!" << std::endl;
	}
	~Entity()
	{
		std::cout << "Entity destroyed!" << std::endl;
	}
	void print() { std::cout << "Message!"; };
};

int main()
{
	std::shared_ptr<int>f1 = std::make_shared<int>(100);

	f1.get()[1] = 10;
	std::cout << f1.get()[1];
}


message it's printed, output: 10
but error:[img]https://i.imgur.com/UPu7VZo.png[/img]

i try in another way:

1
2
3
4
5
6
7
int main()
{
	std::shared_ptr<int>f1 = std::make_shared<int>(100);

	*(f1.get() +1) = 10;
	std::cout << *(f1.get() + 1);
}


same error.

 
std::shared_ptr<Entity[]>f1 = std::make_shared<Entity[]>(new Entity[100]);


i have error...

What i try to make something like this:
1
2
3
4
5
6
7
8
9
int main()
{
	Entity* f1 = new Entity[100];

	f1[0].print();
	f1[1].print();
	f1[2].print();

}


but i want to use smart pointers.


for int i want to make some assigned value like this:
1
2
3
4
5
6
7
8
9
int main()
{
	int* f1 = new int[100];

	f1[0] = 14;
	f1[1] = 20;
	f1[2] = 5;

}


How can i make something like this with smart pointers. I want to use: std::make_shared<Entity>(new Entity[100]) or something like this.

I can try with library like vector or array but i don't want to use this library for the moment. I want to keep my code clear for the moment. After i understand 100% smart pointers i will use array and vector library
Last edited on
See https://stackoverflow.com/questions/13061979/shared-ptr-to-an-array-should-it-be-used
With C++17, shared_ptr can be used to manage a dynamically allocated array. The shared_ptr template argument in this case must be T[N] or T[].
Topic archived. No new replies allowed.