# Creating 'new' elements without using new

**URL:** <https://forum.juce.com/t/creating-new-elements-without-using-new/32458>\
**Category:** General JUCE discussion\
**Created:** [March 23, 2019, 6:22pm UTC](https://forum.juce.com/t/creating-new-elements-without-using-new/32458 "2019-03-23T18:22:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebound](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ebound/32/5800_2.png) [@ebound](https://forum.juce.com/u/ebound)\
**Post date:** [March 23, 2019, 6:22pm UTC](https://forum.juce.com/t/creating-new-elements-without-using-new/32458/1 "2019-03-23T18:22:12Z")

</div>

Hello,

I am working on a project that was following the tutorial, in the tutorial they use the keyword ‘new’ to create new elements and add them, like this

```
 XmlElement* newItem = new XmlElement("ITEM");
 tutorialData->addChildElement(newItem);

```

I’m looking to do this the correct way so I’m changing my statement to be like this:

```
auto newItem = std::make_unique<XmlElement>("ITEM");
 tutorialData->addChildElement(newItem);

```

The issue I think I am encountering is that all of the XmlElement functions will only accept regular pointers and not unique\_pointers, so how would I go about generating this new element as a unique pointer to that object on the heap, but then pass it to the functions like a normal pointer?

the error I am getting is this:

`Error (active)	E0413	no suitable conversion function from "std::unique_ptr<juce::XmlElement, std::default_delete<juce::XmlElement>>" to "juce::XmlElement *" exists`

I think I understand what needs to be done, is instead of using a normal pointer and adding it with ‘new’ i need to use the unique\_pointer that has its lifetime defined by the object it is owned by, but I just need a bit of help with the syntax in order to convert that to the type of pointer that is used in all of the functions everywhere else, thanks in advance for the help.

---

<div class="post-metadata">

**Author:** ![holy-city](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@holy-city](https://forum.juce.com/u/holy-city)\
**Post date:** [March 23, 2019, 6:24pm UTC](https://forum.juce.com/t/creating-new-elements-without-using-new/32458/2 "2019-03-23T18:24:45Z")

</div>

Check the docs:

[https://docs.juce.com/master/classXmlElement.html#a9b9735dd8a98c5af71db4c532cd10a1c](https://docs.juce.com/master/classXmlElement.html#a9b9735dd8a98c5af71db4c532cd10a1c)

`addChildElement` takes ownership of the pointer, so you don’t need to use `std::make_unique`.

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [March 23, 2019, 6:26pm UTC](https://forum.juce.com/t/creating-new-elements-without-using-new/32458/3 "2019-03-23T18:26:51Z")

</div>

However, if you want to wrap it into a unique pointer and pass the ownership to a function that takes a raw pointer later on you want to call `newItem.release()`

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [March 23, 2019, 8:20pm UTC](https://forum.juce.com/t/creating-new-elements-without-using-new/32458/4 "2019-03-23T20:20:20Z")

</div>

This is the correct answer. At the moment, this is the best you can do:

```auto
auto element = std::make_unique<XmlElement>("ITEM");
tutorialData->addChildElement(element.release());

```

However, in the future, I believe that the APIs of these classes will be updated so that they properly support smart pointers, in which case you would write this:

```auto
auto element = std::make_unique<XmlElement>("ITEM");
tutorialData->addChildElement(std::move(element));

```
