Skip to content

Instantly share code, notes, and snippets.

@FONQRI
Created January 23, 2018 12:21
Show Gist options
  • Save FONQRI/308a97fc520d2d30779e0d4f9dfebb44 to your computer and use it in GitHub Desktop.
Save FONQRI/308a97fc520d2d30779e0d4f9dfebb44 to your computer and use it in GitHub Desktop.
vector of shared_prt or shared_ptr of vector in c++
#include <iostream>
#include <memory>
#include <vector>
class TestClass {
public:
TestClass(int id);
~TestClass();
public:
int id;
};
void testNormalFunction(std::vector<TestClass> vector) {}
void testSharedVectorFunction(std::shared_ptr<std::vector<TestClass>> vector) {}
void testVectorSharedFunction(std::vector<std::shared_ptr<TestClass>> vector) {}
int main()
{
std::vector<TestClass> normalVector;
normalVector.push_back(TestClass(1));
testNormalFunction(normalVector);
std::shared_ptr<std::vector<TestClass>> sharedVector(
std::make_shared<std::vector<TestClass>>());
sharedVector->push_back(TestClass{2});
testSharedVectorFunction(sharedVector);
std::vector<std::shared_ptr<TestClass>> vectorShared;
vectorShared.push_back(std::make_shared<TestClass>(3));
testVectorSharedFunction(vectorShared);
return 0;
}
TestClass::TestClass(int id) : id(id)
{
std::clog << "created : " << id << std::endl;
}
TestClass::~TestClass() { std::clog << "destroyed : " << id << std::endl; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment