# How can I create a new object from a pointer?

**URL:** <https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551>\
**Category:** Plugins\
**Tags:** newobject, unreal-engine, pointer, Plugins, question, CPP\
**Created:** [May 5, 2015, 6:54pm UTC](https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551 "2015-05-05T18:54:49Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_a4cc7aca](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a4cc7aca](https://forums.unrealengine.com/u/anonymous_user_a4cc7aca)\
**Post date:** [May 5, 2015, 6:54pm UTC](https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551/1 "2015-05-05T18:54:49Z")

</div>

I cant seem to create a new object from a nested pointer for my plugin. My code is setup like so: I create a query object, the query object has a model property. I need to return an array of models. Currently I’m stuck creating an array of models all pointing to the same thing. How would I create a new instance of the model in a for loop?

UDatabaseCommunicatorModel is its own .cpp, and Model is its own .cpp  
Both are UObjects

Code Below:

TArray UDatabaseCommunicatorQuery::GetQueryResults(UDatabaseCommunicatorQuery\* Query)

{

```
TArray<UDatabaseCommunicatorModel*> ModelArray;

//For Loop{
    UDatabaseCommunicatorModel Model = *Query->QueryModel;
    //Add the new Model object to the array
}

return ModelArray;

```

}  
This code does not compile because VS 2013 is stating that GENERATED\_UCLASS\_BODY() is inaccessible in the Model .h  
Everything is set to public

Thanks for any help

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_41845f4a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_41845f4a](https://forums.unrealengine.com/u/anonymous_user_41845f4a)\
**Post date:** [May 5, 2015, 7:24pm UTC](https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551/2 "2015-05-05T19:24:56Z")

</div>

Hi Stunt ,

Can you please provide me with your Model.h and Model.cpp files? If GENERATED\_UCLASS\_BODY() is inaccessible, the problem may be somewhere outside of the function you provided.

-

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a4cc7aca](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a4cc7aca](https://forums.unrealengine.com/u/anonymous_user_a4cc7aca)\
**Post date:** [May 5, 2015, 7:38pm UTC](https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551/3 "2015-05-05T19:38:47Z")

</div>

Files are attached thx for the help.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_41845f4a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_41845f4a](https://forums.unrealengine.com/u/anonymous_user_41845f4a)\
**Post date:** [May 5, 2015, 8:03pm UTC](https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551/4 "2015-05-05T20:03:55Z")

</div>

This most likely isn’t related to the problem but since 4.6, GENERATED\_UCLASS\_BODY() has been replaced with GENERATED\_BODY, just FYI.

I should have been more clear when asking but can you send me the actual code files themselves? It would also help to have all of them as you are including some other files. This way I can put them right into a project and run them through the compiler and get the errors as I attempt to identify the issue.

-

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a4cc7aca](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a4cc7aca](https://forums.unrealengine.com/u/anonymous_user_a4cc7aca)\
**Post date:** [May 5, 2015, 8:34pm UTC](https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551/5 "2015-05-05T20:34:07Z")

</div>

I attached the query header and cpp. Notice the intellisense on line 104 in the query cpp where i’m trying to create a new Model. Im new to C++. I’m actually a .net developer trying to create a ODBC plugin for unreal. C# to C++ is a pain lol. Thx again for the help man.

---

<div class="post-metadata">

**Author:** ![MickD777](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MickD777](https://forums.unrealengine.com/u/MickD777)\
**Post date:** [May 5, 2015, 8:35pm UTC](https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551/6 "2015-05-05T20:35:24Z")

</div>

It’s not replaced, it does different things. GENERATED\_UCLASS\_BODY() when you need the Objectinitializer in your constructor, GENERATED\_BODY when you want to define your class constructor yourself…

GENERATED\_UCLASS\_BODY() macro ends in public scope  
GENERATED\_BODY macro ends in private

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a4cc7aca](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a4cc7aca](https://forums.unrealengine.com/u/anonymous_user_a4cc7aca)\
**Post date:** [May 5, 2015, 8:35pm UTC](https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551/7 "2015-05-05T20:35:46Z")

</div>

And I made the change to GENERATED\_BODY() in all my plugin classes

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_41845f4a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_41845f4a](https://forums.unrealengine.com/u/anonymous_user_41845f4a)\
**Post date:** [May 5, 2015, 8:37pm UTC](https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551/8 "2015-05-05T20:37:03Z")

</div>

Ah, thank you for that information. I was under the assumption it was simply replaced.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_41845f4a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_41845f4a](https://forums.unrealengine.com/u/anonymous_user_41845f4a)\
**Post date:** [May 5, 2015, 9:12pm UTC](https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551/9 "2015-05-05T21:12:39Z")

</div>

I believe I may of pinpointed the issue. The line you pointed out that has the intellisense error is a little off. The correct way should be:

UDatabaseCommunicatorModel \*Model = UDatabaseCommunicatorModel();

Or if that doesn’t work:

UDatabaseCommunicatorModel \*Model = new UDatabaseCommunicatorModel();

This should create a UDatabaseCommunicatorModel pointer to a new UDatabaseCommunicatorModel object. You could also put a value in the parentheses if you wish to initialize the new object. This is how it would be done in normal C++ (I’m not exactly sure how friendly UE4 is with the ‘new’ operator). I’m not sure exactly how this will react to UProperties either.

Hopefully this will help, if not, please let me know.

-

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a4cc7aca](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a4cc7aca](https://forums.unrealengine.com/u/anonymous_user_a4cc7aca)\
**Post date:** [May 5, 2015, 9:12pm UTC](https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551/10 "2015-05-05T21:12:41Z")

</div>

Considering this has something to do with GENERATED\_BODY not being public, I created a new post [GENERATED\_BODY() is inaccessible - Plugins - Epic Developer Community Forums](https://answers.unrealengine.com/questions/223203/generated-body-is-inaccessible.html)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a4cc7aca](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a4cc7aca](https://forums.unrealengine.com/u/anonymous_user_a4cc7aca)\
**Post date:** [May 5, 2015, 9:18pm UTC](https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551/11 "2015-05-05T21:18:36Z")

</div>

Go Fish ☹

 ![41499-updated+query+cpp.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/b/1/5b1a4022cc0a16ac85cab819ddb68d41cac1f4c1.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a4cc7aca](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a4cc7aca](https://forums.unrealengine.com/u/anonymous_user_a4cc7aca)\
**Post date:** [May 5, 2015, 9:26pm UTC](https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551/12 "2015-05-05T21:26:53Z")

</div>

UDatabaseCommunicatorModel \*Model = NewObject(Query-\>QueryModel);

will compile, but my Model has an array of columns that is not being copied to the new Model

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_c308fbca](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_c308fbca](https://forums.unrealengine.com/u/anonymous_user_c308fbca)\
**Post date:** [May 6, 2015, 12:23am UTC](https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551/13 "2015-05-06T00:23:45Z")

</div>

Have you tried ConstructObject? (Not sure if this will help though.)

> **[UObject Instance Creation](https://docs.unrealengine.com/5.1/en-US/creating-objects-in-unreal-engine/)**
>
> Methods of creating new instances of Objects in gameplay code.

Or alternatively create your own constructor to add values as needed.

```
UDatabaseCommunicatorModel(const FObjectInitializer& ObjectInitializer);

```

> **[4.6 constructor changes, am confused](https://forums.unrealengine.com/showthread.php?52236-4-6-constructor-changes-am-confused&p=181552&viewfull=1#post181552)**
>
> Hi guys, I am probably not the brightest person in the world… I just downloaded and compiled the 4.6 engine, and creating a c++ class from the editor creates files, but the cpp files has no constructor as before. Now i here on the forum that the...

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a4cc7aca](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a4cc7aca](https://forums.unrealengine.com/u/anonymous_user_a4cc7aca)\
**Post date:** [May 7, 2015, 11:08pm UTC](https://forums.unrealengine.com/t/how-can-i-create-a-new-object-from-a-pointer/317551/14 "2015-05-07T23:08:20Z")

</div>

This is what i was looking for:

UDatabaseCommunicatorModel\* Model = Cast(Query-\>QueryModel);

Normal C++ casting doesnt work on UObjects
