I have a class, that contains a struct. I have a method on the class that creates a new object of this struct, return it as a pointer.
I have another method in this class that takes a pointer to this struct and prints out it's data.
Only problem is, some weird text shows up in the console when I try to print it out.
Code example (not actual code, but the principle of it):
// Header
class TestClass
{
public:
struct TestStruct
{
int ID;
string Name;
};
TestClass::TestStruct* CreateStruct(string name, int id);
void PrintStruct(TestClass:TestStruct* testStruct);
}
// C++ File
TestClass::TestStruct* TestClass::CreateStruct(string name, int id)
{
TestStruct testStruct;
testStruct.ID = id;
testStruct.Name = name;
TestClass::TestStruct *pStruct = &testStruct;
return pStruct;
};
void TestClass::PrintStruct(TestClass::TestStruct* testStruct)
{
cout << (testStruct)->ID << "\n";
cout << (testStruct)->Name << "\n";
};
int Main()
{
TestClass tClass;
tClass.PrintStruct(tClass.CreateStruct("A name", 1));
}