/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
// -----------------------
// https://stackoverflow.com/questions/58970041/segmentation-fault-with-unique-ptr
// ------------------------
*******************************************************************************/
#include <iostream>
#include <string>
#include <memory>
#include <memory>
using namespace std;
class S {
std::string m_param;
public:
S(std::string param) : m_param(param){ cout << "constructing S(" << param << ")" << endl; }
~S(){ cout << "destroy S" << std::endl; }
string const get() {return m_param;}
};
class A {
string m_param;
public:
A(std::string param) : m_param(param+"(") { std::cout << "constructing A(" << param << ")" << endl; }
~A(){ cout << "destroy A" << std::endl; }
void Add(S *song) { m_param = m_param+song->get(); }
string const get() {return m_param + ")";}
};
void func()
{
std::unique_ptr<A> album = std::unique_ptr<A>{new A("Album")};
std::unique_ptr<S> song = std::unique_ptr<S>{new S("Constructing a Song")};
album->Add(song.get());
cout << "returned from Add() without errors" << endl;
}
int main()
{
S song("Hello World");
cout << song.get() << endl;
func();
cout << "returned from func() without errors" << endl;
return 0;
}