I am at a loss as to why I cannot
- Declare an object pointer set to 0
- Pass the object pointer to a function that will create the object
- The function sets the passed pointer to the newly allocated object
Example
#include <iostream>
using namespace std;
class Unit {
public:
Unit();
~Unit();
string name;
};
Unit::Unit(){}
Unit::~Unit(){}
void DoFoo(Unit *unit);
int main()
{
Unit *unit = 0;
DoFoo(unit);
cout << unit->name;
return 0;
}
void DoFoo(Unit *unit){
unit = new Unit();
unit->name = "hi";
}
Result is Segment fault...
In my head I am simply declaring a pointer, passing the address to the function, and then the function is setting the address to the address of the allocated object...