-
July 27th, 2016, 11:32 PM
#1
How to make two pointers the same?
Unfortunately, when the NavigationSystem is added to the Truck object,
it is turned to another type of pointer (a unique_ptr), which is another copy and type
of the original pointer,
When I take it back thru dynamic_casting, I'll get a different one.
when the component is updated, the copy in
the NavigationSysMgr is not updated.
How can I make sure the copy in the NavigationSysMgr is updated whenever
the other copy in the Truck class (the component container) changes?
Thanks
Jack
Code:
NavigationSystem* l_NavigationSys = new NavigationSystem("Truck");
this->AddComponent(ComponentManager::getInstance()->AddComponent(l_NavigationSys));
NavigationSysMgr::getInstance()->AddNavigationSys(this->FindCompoentByType<NavigationSystem>());
template<typename T>
inline void AddComponent(T* l_component)
{
l_component->m_simObject = this;
m_components.push_back(std::move(std::make_unique<T>(*l_component)));
l_component->Start();
}
template<typename T>
T* FindCompoentByType()
{
for (component_vector_itr itr = m_components.begin(); itr != m_components.end(); ++itr)
{
if (T* l_type = dynamic_cast<T*>((*itr).get()))
{
return l_type;
}
}
return NULL;
};
Last edited by lucky6969b; July 27th, 2016 at 11:45 PM.
-
July 28th, 2016, 02:13 AM
#2
Re: How to make two pointers the same?
What is m_components? A vector with unique_ptrs?
You must learn how to use smart pointers correctly!
AddCompenent will move a unique component into that vector, but that's not reflected by the interface.
If you want uniqueness, it should look something like:
Code:
AddComponent( std::unique_ptr<T> component )
{
assert( component != nullptr );
component->m_simObject = this;
component->Start();
m_components.emplace_back( std::move(component) );
}
...and l_NavigationSys should be a unique_ptr that you move when you call AddComponent.
I would recommend that you NEVER dereference a variable when calling make_unique.
std::make_unique<T>(*something); // DON'T DO IT!!
If you do this in other parts of your code, you should go back and fix the design immediately.
For a deeper explanation how to write pointers as parameters, check out the solution to question #3 in this link:
https://herbsutter.com/2013/06/05/go...er-parameters/
Last edited by TubularX; July 28th, 2016 at 03:07 AM.
-
July 28th, 2016, 03:34 AM
#3
Re: How to make two pointers the same?
Hello,
If I move the unique_ptr around, is it safe to leave a raw copy in the ComponentManager?
Code:
template<typename T>
std::unique_ptr<T> AddComponent(std::unique_ptr<T> l_component)
{
assert(l_component != nullptr);
m_currentComponentCount++;
if (m_currentComponentCount == m_currentComponentsSize - 1)
{
ResizeComponents();
}
if (Renderer* renderable = dynamic_cast<Renderer*>(l_component.get()))
{
this->m_renderComponents.push_back(renderable);
}
this->m_components[m_currentComponentCount - 1] = l_component.get();
return l_component;
}
-
July 28th, 2016, 03:52 AM
#4
Re: How to make two pointers the same?
It's only safe if you can guarantee that the lifetime of the original object outlasts all references to it.
When the T object is finally destroyed, there must be no invalid pointers or references left behind.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|