-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsharedPtr.cpp
More file actions
executable file
·70 lines (67 loc) · 1.94 KB
/
sharedPtr.cpp
File metadata and controls
executable file
·70 lines (67 loc) · 1.94 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* ATIVIDADE 3 - SI300/A
* DEMONSTRAÇÃO DE USO DO SHARED_PTR
* UNICAMP - FACULDADE DE TECNOLOGIA* 2016
*/
#include <cstdlib>
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class Inteiros{
public:
vector <shared_ptr<int>> setNumero();
/*
* PONTEIRO SHARED_PTR PERMITE CÓPIA E PODE SER USADO COMO PARÂMETRO
* PARA FUNÇÕES; SEGUE ABAIXO:
*/
void getNumero(vector <shared_ptr<int>>);
void Clear(vector <shared_ptr<int>>);
};
void Inteiros::Clear(vector <shared_ptr<int>> inteiros){
cout << endl;
cout << "Limpando lista" << endl;
//PARA AS LINHAS DE CÓDIGO ABAXIO, VALE A MESMA OBSERVAÇÃO FEITA NOS
//COMENTARIOS DO CÓDIGO DE EXEMPLO DE USO DO unique_ptr
/*while (scan != inteiros.end())
{
delete (*scan);
(*scan) = NULL;
scan++;
}*/
inteiros.clear();
cout << "Finalizando" << endl;
}
void Inteiros::getNumero(vector <shared_ptr<int>> inteiros){
vector<shared_ptr<int>>::iterator scan = inteiros.begin();
cout << endl << "Imprimindo vetor:" << endl;
while (scan != inteiros.end()){
cout << **scan << endl;
scan++;
}
}
vector <shared_ptr<int>> Inteiros::setNumero(){
vector <shared_ptr<int>> inteiros;
for (int i = 1; i < 5; i++){
shared_ptr <int> x(new int);
cout << "Digite o " << i << "o número" << endl;
cin >> *x;
if (*x != 0){
inteiros.insert(inteiros.end(), x); //PONTEIRO SHARED_PTR PERMITE CÓPIA. NÃO NECESSITA move
}
else{
cout << "Número zero não permitido. Tente outro número." << endl;
i--;
}
}
return inteiros;
}
int main(int argc, char** argv){
Inteiros numeros;
vector <shared_ptr<int>> inteiros;
cout << "Digite 4 números" << endl;
inteiros = numeros.setNumero();
numeros.getNumero(inteiros); //RECURSO COMPARTILHADO
numeros.Clear(inteiros); //RECURSO COMPARTILHADO
return 0;
}