i have that c++ code
#include <iostream>
#include <vector>
#include <memory>
struct Foo{
int* a;
Foo(std::vector<int> vec)
{
a = &(vec.front());
}
};
struct Bar {
std::unique_ptr<int> a;
Bar(std::vector<int> vec)
{
a = std::make_unique<int>(vec.front());
}
};
int main() {
std::vector<int> vec = {42};
Foo foo(vec);
Bar bar(vec);
// returns 0, but should 42
std::cout << *foo.a << std::endl;
// but this returns 42
std::cout << *bar.a << std::endl;
return 0;
}
why does *foo.a returns 0, instead of 42? What am i doing wrong?