My thoughts on pointers

I have 2+ years of experience in c++ but still this syntax is daunting and very when Ben told us that the * was not referencing the pointer I was actually in awe at the complexity of Unreal for a beginner who has no experience with c++.

That would be the result of operator overloading.

//FString operator *
FORCEINLINE const TCHAR* operator*() const
{
	return Data.Num() ? Data.GetData() : TEXT("");
}

interestingly enough the dereference still worked
FString compName = GetName();
FString* stringPtr = &compName;
UE_LOG(LogTemp, Error, TEXT(“Component Name: %s”), *( *stringPtr) );

I guess cuz it doesnt know its a string until it becomes dereferenced

That’s because you are dereferencing the FString pointer to get the FString and then using the dereference operator on that. Basically you are just adding an extra step in that example you provided (creating a pointer to compName)

I have a question:
FString ObjectName = GetOwner( ) -> GetName( );
how can function call the function GetOwner( ) -> GetName( ); like this

Let me answer that with a question. Why shouldn’t it work? GetOwner returns an AActor*, why can’t you call a function from that?

class A {
protected:
string name;
int age;
public:

  A() : name("Leo"), age(31) {}
 //A(string n= "leo", int v= 31) { name = n; age = v; }
 string getName() { return name; }
 int getAge() { return age; }
 A *getObject() const { A a; return &a; }

};

int main() {

A *b,c;
b = &c;
string s = getObject()->getName();
 cout << s << endl;
return 0;

}

because this simple code isn’t work and the problem is:
string s = getObject()->getName(); this order

The problem isn’t the order, it’s the design of it. You’re calling getObject from nowhere, you aren’t calling it from an object. You are also returning an address of a local variable in getObject().

class A 
{
	std::string name;
	int age;
public:
	A() : name("Leo"), age(31) {}

	std::string getName() { return name; }
	int getAge() { return age; }
	A *getObject() const { A a; return &a; } //a will be destroyed at the end of this call.
};

int main() 
{
	A a;
	std::string s = a.getObject()->getName();
	std::cout << s << std::endl;
	return 0;
}

I believe you were aiming for something like

class A 
{
	std::string name;
public:
	A() : name("bob") {}
	std::string getName() { return name; }
};

class B
{
	A* a;
public:
	B() { a = new A; }
	~B() { delete a; }
	A* getObject() { return a; }
};

int main() 
{
	B b;
	std::string s = b.getObject()->getName();
}

P.S don’t use new/delete, use std::unique_ptr or std::shared_ptr instead.

//a will be destroyed at the end of this call.
ahh! you meaning that when the function finished it’s work steck (memory) will be destroyed and object destroye also. do you???

Yes

A* getObject() const 
{
    A a; //created on the stack, destroyed at the end of the scope
    return &a;
} //`a` destroyed, anything that is referring to `a` is now pointing to invalid memory.

class A {
protected:
string name;
int age;
public:

  A() : name("Leo"), age(31) {}
 //A(string n= "leo", int v= 31) { name = n; age = v; }
 string getName() { return name; }
 int getAge() { return age; }
 friend A getObject();

};

A getObject() { A a; return a; }

int main() {

string s = getObject().getName();
cout << s << endl;
return 0;

}

but this code works fine why?
if function return value not address it’s not destroyed?

Because that’s a copy, returning by value means you a creating a copy.

thank you DanM so much
and last just now :slight_smile:

P.S don’t use new/delete, use std::unique_ptr or std::shared_ptr instead.

what means unique_ptr and shared_ptr and why not use new/delete?

In my second example above where I use new and delete, that’s managing dynamic memory yourself. If an exception is throw before B’s deconstructor is called then a is never deleted and freed from memory.

In C++11 smart pointers were introduced so you don’t have to manage dynamic memory yourself. Refactoring my earlier code to use unique_ptr instead

class A
{
	std::string name;
public:
	A() : name("bob") {}
	std::string getName() { return name; }
};

class B
{
	std::unique_ptr<A> a;
public:
	B() { a = std::make_unique<A>(); }
	A* getObject() { return a.get(); }
};

int main()
{
	B b;
	std::string s = b.getObject()->getName();
}

So now I don’t have to worry about calling delete on a on the B object, since it’s managed by the smart pointer. I can still use raw pointers ala A* getObject by calling .get on the smart pointer. The reason behind returning a raw pointer is because whoever calls getObject isn’t taking a part in ownership of a.

Further reading: https://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one

than you DanM you are real friend :slight_smile:

Privacy & Terms