Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to use unique_ptr in C++

6 Answers

0 votes
#include <iostream>
#include  <memory>

using std::cout;
using std::endl;

struct S
{
	int N;
	S(int i) :N(i)
	{
		cout << "Constructor" << endl;
	}
	~S()
	{
		cout << "Destructor" << endl;
	}
};

int main()
{
	std::unique_ptr<S> UPtr(new S(100));

	int i = UPtr->N;

	cout << i << endl;

	return 0;
}


/*
run:

Constructor
100
Destructor

*/

 



answered Feb 1, 2018 by avibootz
0 votes
#include <iostream>
#include  <memory>

using std::cout;
using std::endl;

struct S
{
	int N;
	S(int i) :N(i)
	{
		cout << "Constructor" << endl;
	}
	~S()
	{
		cout << "Destructor" << endl;
	}
};

int main()
{
	std::unique_ptr<S> UPtr(new S(100));

	cout << UPtr->N << endl;

	return 0;
}


/*
run:

Constructor
100
Destructor

*/

 



answered Feb 1, 2018 by avibootz
0 votes
#include <iostream>
#include  <memory>

using std::cout;
using std::endl;

struct S
{
	int N;
	S(int i) :N(i)
	{
		cout << "Constructor" << endl;
	}
	~S()
	{
		cout << "Destructor" << endl;
	}
};

int main()
{
	std::unique_ptr<S> UPtr(new S(100));

	UPtr.reset();

	if (UPtr == nullptr)
		cout << "UPtr is  empty" << endl;

	return 0;
}


/*
run:

Constructor
Destructor
UPtr is  empty

*/

 



answered Feb 1, 2018 by avibootz
0 votes
#include <iostream>
#include  <memory>

using std::cout;
using std::endl;

struct S
{
	int N;
	S(int i) :N(i)
	{
		cout << "Constructor" << endl;
	}
	~S()
	{
		cout << "Destructor" << endl;
	}
};

int main()
{
	std::unique_ptr<S> UPtr(new S(100));

	std::unique_ptr<S> UPtr2 = std::move(UPtr);

	cout << UPtr2->N << endl;

	return 0;
}


/*
run:

Constructor
100
Destructor

*/

 



answered Feb 1, 2018 by avibootz
0 votes
#include <iostream>
#include  <memory>

using std::cout;
using std::endl;

struct S
{
	int N;
	S(int i) :N(i)
	{
		cout << "Constructor" << endl;
	}
	~S()
	{
		cout << "Destructor" << endl;
	}
};

int main()
{
	std::unique_ptr<S> UPtr(new S(100));

	std::unique_ptr<S> UPtr2(new S(333));

	cout << UPtr->N << endl;
	cout << UPtr2->N << endl;

	return 0;
}


/*
run:

Constructor
Constructor
100
333
Destructor
Destructor

*/

 



answered Feb 1, 2018 by avibootz
0 votes
#include <iostream>
#include  <memory>

using std::cout;
using std::endl;

struct S
{
	int N;
	S(int i) :N(i)
	{
		cout << "Constructor" << endl;
	}
	~S()
	{
		cout << "Destructor" << endl;
	}
};

int main()
{
	std::unique_ptr<S> UPtr(new S(100));

	S *p = UPtr.release();

	if (UPtr == nullptr)
		cout << "UPtr is empty" << endl;

	cout << p->N << endl;

	delete p;

	return 0;
}


/*
run:

Constructor
UPtr is empty
100
Destructor

*/

 



answered Feb 1, 2018 by avibootz

Related questions

...