I'm having some issue with returning vectors of objects of a class in functions because everytime my destructor erases the data twice and all the code just dies when the functions ends
here a simple code I wrote just to show my problem:
#include <iostream>
#include <vector>
using namespace std;
class identity{
public:
string name;
identity(string Name);
~identity();
};
vector<identity> function();
int main(){
function();
cout << "Hello world!";
}
identity::identity(string Name)
: name{Name}{
cout << "Object created!" << endl;
}
identity::~identity(){
cout << "Object " << name << " destroyed!" << endl;
}
vector<identity> function(){
identity me("Isaias");
}
in this case the cout "Hello world" doesn't work and the program always ends with "Object" without displaying the name like this:
Object created!
Object Isaias destroyed!
Object
and then the program just stops. I kind of fixed the problem by seting the type of the function to "void" or anything else instead of "vector" but I'd like to know why this problem occurs. Also I'm new to programming in general and in this community so I'm sorry if I'm not doing this in the right way.
I'd like to thank you all for your attention before anything and sorry again if i am messing everything up here.