I am currently trying to learn how to use smart pointers. However while doing some experiments I discovered the following situation for which I could not find a satifying solution:
Imagine you have an object of class A being parent of an object of class B (the child), but both should know each other:
class A;
class B;
class A
{
public:
void addChild(std::shared_ptr<B> child)
{
children->push_back(child);
// How to do pass the pointer correctly?
// child->setParent(this); // wrong
// ^^^^
}
private:
std::list<std::shared_ptr<B>> children;
};
class B
{
public:
setParent(std::shared_ptr<A> parent)
{
this->parent = parent;
};
private:
std::shared_ptr<A> parent;
};
The question is how can an object of class A pass a std::shared_ptr of itself (this) to its child?
There are solutions for Boost shared pointers (Getting a boost::shared_ptr for this), but how to handle this using the std:: smart pointers?
There is std::enable_shared_from_this just for this purpose. You inherit from it and you can call .shared_from_this() from inside the class. Also, you are creating circular dependencies here that can lead to resource leaks. That can be resolved with the use of std::weak_ptr. So your code might look like this (assuming children rely on existence of parent and not the other way around):
class A;
class B;
class A
: public std::enable_shared_from_this<A>
{
public:
void addChild(std::shared_ptr<B> child)
{
children.push_back(child);
// like this
child->setParent(shared_from_this()); // ok
// ^^^^^^^^^^^^^^^^^^
}
private:
// note weak_ptr
std::list<std::weak_ptr<B>> children;
// ^^^^^^^^
};
class B
{
public:
void setParent(std::shared_ptr<A> parent)
{
this->parent = parent;
}
private:
std::shared_ptr<A> parent;
};
Note however, that calling .shared_from_this() requires that this is owned by std::shared_ptr at the point of call. This means that you cannot create such object on stack anymore, and generally cannot call .shared_from_this() from within a constructor or destructor.
You have several problems in you design, that seem to stem from you misunderstanding of smart pointers.
Smart pointers are used to declare ownership. You are breaking this by declaring that both the parents owns all children, but also that each child own it's parent. Both can't be true.
Also, you are returning a weak pointer in getChild(). By doing so, you are declaring that the caller shouldn't care about the ownership. Now this can be very limiting, but also by doing so, you must make sure that the child in question won't get destroyed while any weak pointers are still held, if you would use a smart pointer, it would get sorted out by itself.
And the final thing. Usually, when you are accepting new entities, you should usually accept raw pointers. Smart pointer can have their own meaning for swapping children between parents, but for general usage, you should accept raw pointers.
Related
I am currently trying to learn how to use smart pointers. However while doing some experiments I discovered the following situation for which I could not find a satifying solution:
Imagine you have an object of class A being parent of an object of class B (the child), but both should know each other:
class A;
class B;
class A
{
public:
void addChild(std::shared_ptr<B> child)
{
children->push_back(child);
// How to do pass the pointer correctly?
// child->setParent(this); // wrong
// ^^^^
}
private:
std::list<std::shared_ptr<B>> children;
};
class B
{
public:
setParent(std::shared_ptr<A> parent)
{
this->parent = parent;
};
private:
std::shared_ptr<A> parent;
};
The question is how can an object of class A pass a std::shared_ptr of itself (this) to its child?
There are solutions for Boost shared pointers (Getting a boost::shared_ptr for this), but how to handle this using the std:: smart pointers?
There is std::enable_shared_from_this just for this purpose. You inherit from it and you can call .shared_from_this() from inside the class. Also, you are creating circular dependencies here that can lead to resource leaks. That can be resolved with the use of std::weak_ptr. So your code might look like this (assuming children rely on existence of parent and not the other way around):
class A;
class B;
class A
: public std::enable_shared_from_this<A>
{
public:
void addChild(std::shared_ptr<B> child)
{
children.push_back(child);
// like this
child->setParent(shared_from_this()); // ok
// ^^^^^^^^^^^^^^^^^^
}
private:
// note weak_ptr
std::list<std::weak_ptr<B>> children;
// ^^^^^^^^
};
class B
{
public:
void setParent(std::shared_ptr<A> parent)
{
this->parent = parent;
}
private:
std::shared_ptr<A> parent;
};
Note however, that calling .shared_from_this() requires that this is owned by std::shared_ptr at the point of call. This means that you cannot create such object on stack anymore, and generally cannot call .shared_from_this() from within a constructor or destructor.
You have several problems in you design, that seem to stem from you misunderstanding of smart pointers.
Smart pointers are used to declare ownership. You are breaking this by declaring that both the parents owns all children, but also that each child own it's parent. Both can't be true.
Also, you are returning a weak pointer in getChild(). By doing so, you are declaring that the caller shouldn't care about the ownership. Now this can be very limiting, but also by doing so, you must make sure that the child in question won't get destroyed while any weak pointers are still held, if you would use a smart pointer, it would get sorted out by itself.
And the final thing. Usually, when you are accepting new entities, you should usually accept raw pointers. Smart pointer can have their own meaning for swapping children between parents, but for general usage, you should accept raw pointers.
I am currently trying to learn how to use smart pointers. However while doing some experiments I discovered the following situation for which I could not find a satifying solution:
Imagine you have an object of class A being parent of an object of class B (the child), but both should know each other:
class A;
class B;
class A
{
public:
void addChild(std::shared_ptr<B> child)
{
children->push_back(child);
// How to do pass the pointer correctly?
// child->setParent(this); // wrong
// ^^^^
}
private:
std::list<std::shared_ptr<B>> children;
};
class B
{
public:
setParent(std::shared_ptr<A> parent)
{
this->parent = parent;
};
private:
std::shared_ptr<A> parent;
};
The question is how can an object of class A pass a std::shared_ptr of itself (this) to its child?
There are solutions for Boost shared pointers (Getting a boost::shared_ptr for this), but how to handle this using the std:: smart pointers?
There is std::enable_shared_from_this just for this purpose. You inherit from it and you can call .shared_from_this() from inside the class. Also, you are creating circular dependencies here that can lead to resource leaks. That can be resolved with the use of std::weak_ptr. So your code might look like this (assuming children rely on existence of parent and not the other way around):
class A;
class B;
class A
: public std::enable_shared_from_this<A>
{
public:
void addChild(std::shared_ptr<B> child)
{
children.push_back(child);
// like this
child->setParent(shared_from_this()); // ok
// ^^^^^^^^^^^^^^^^^^
}
private:
// note weak_ptr
std::list<std::weak_ptr<B>> children;
// ^^^^^^^^
};
class B
{
public:
void setParent(std::shared_ptr<A> parent)
{
this->parent = parent;
}
private:
std::shared_ptr<A> parent;
};
Note however, that calling .shared_from_this() requires that this is owned by std::shared_ptr at the point of call. This means that you cannot create such object on stack anymore, and generally cannot call .shared_from_this() from within a constructor or destructor.
You have several problems in you design, that seem to stem from you misunderstanding of smart pointers.
Smart pointers are used to declare ownership. You are breaking this by declaring that both the parents owns all children, but also that each child own it's parent. Both can't be true.
Also, you are returning a weak pointer in getChild(). By doing so, you are declaring that the caller shouldn't care about the ownership. Now this can be very limiting, but also by doing so, you must make sure that the child in question won't get destroyed while any weak pointers are still held, if you would use a smart pointer, it would get sorted out by itself.
And the final thing. Usually, when you are accepting new entities, you should usually accept raw pointers. Smart pointer can have their own meaning for swapping children between parents, but for general usage, you should accept raw pointers.
Is this method of passing and storing this to a child object still considered 'acceptable' when using C++17, or is there a more appropriate method, in line with the language and standard?
I am specifically asking regarding passing and storing the Parent object as a plain pointer.
class Child
{
public:
void SetParent(Parent* p)
{
_parent = p;
}
private:
Parent* _parent;
};
class Parent
{
public:
void MyMethod()
{
Child c;
c.SetParent(this);
}
};
Post-C++11 you can use std::weak_ptr<Parent>, assuming you're using std::shared_ptr<Parent> (and inherit from std::enable_shared_from_this in order to generate a std::shared_ptr or std::weak_ptr internally).
Aside from that, yes, it's still acceptable to use a raw pointer to represent lack of ownership or back pointer (until the committee adds some kind of std::owned_ptr<T> / std::ptr_view<T> class).
I am having trouble with RobotControl class members. The UML specifies the relation between RobotControl’s position and RangeSensor as composition. Doesn't using pointers for them, make them aggregation? How should I declare - create these members with respect to UML, or has UML mistaken?
Pointers in C++ can be used for both aggregation and composition. The distinction is, as correctly noted by Douglas, whether the lifetime of the objects is interconnected. In other words: Is the child destroyed when the parent is destroyed? The answer Yes stands for composition, No for aggregation.
How do we distinguish these cases in a C++ code?
Pointers in C++ can mean the ownership of another (dynamically created) object, or just refer to an object owned by someone else. Let’s show the differences in examples. I’ll explain why pointers can be useful for each type of relationship.
Aggregation with a pointer
In this case, it is fine to only forward-declare the class Child before class Parent declaration and the child member can be set and re-set during the lifetime of the Parent.
class Child;
class Parent
{
public:
Parent(Child* ch) : child(ch) {}
Parent() : child(NULL) {}
void setChild(Child* ch) { child = ch; }
private:
Child* child;
};
Composition with a pointer
The longest example shows that we can dynamically create and destroy the child using a pointer. The child’s lifetime is strongly interconnected with the Parent. However, we can still swap children during the lifetime of the Parent thanks to pointers. This solution also allows to only forward-declare class Child in the header file unlike the alternative below.
// --- header file
class Child;
class Parent
{
public:
Parent();
~Parent();
void renewChild();
private:
Child* child;
};
// --- source file
#include "child.h"
Parent::Parent()
: child(new Child)
{
}
Parent::~Parent()
{
delete child;
}
void Parent::renewChild()
{
delete child;
child = new Child;
}
Disclaimer
This example is a subject to the Rule of three/five/zero. I am intentionally letting the implementation of missing recommended methods up to the user, keeping this answer dialect-agnostic and as simple as possible.
Composition without pointers
Instead of writing constructor and destructor manually, you can just declare child in the class declaration and let the compiler to do the construction and destruction for you. This is valid as long as the class Child’s constructor requires no parameters (otherwise you’d need to write class Parent’s constructor manually) and the class Child is fully declared before the declaration of class Parent.
#include "child.h"
class Parent
{
private:
Child child;
};
Aggregation without pointers
To be complete, the alternative to using pointers for aggregation is using a reference. However, this prevents swapping children during the lifetime of the Parent object.
class Child;
class Parent
{
public:
Parent(Child& ch) : child(ch) {}
private:
Child& child;
};
I have a type heirarchy:
class Object {
...
};
class Node : public Object {
...
};
class Leaf : public Object {
...
};
class Something : public Node {
...
};
class SomethingElse : public Leaf {
...
};
In other words, absolutely every class inherits Object either directly or indirectly.
The constructor for every object is in the following format:
ClassType(Object * parent)
However, by design only a Node can be a parent, while a Leaf is a terminating node;
In the moment, in every constructor I am doing the following:
Node * p = dynamic_cast<Node *>(parent);
if (p) p->adopt(this);
else qDebug() << "for some reason cast failed";
So, my primary question is this - considering that every parent object can only be a Node or derived from Node, is it safe to use static_cast instead? Naturally, the primary concern is performance, right now I get about 4 million nodes created per second, which does not seem like a bottleneck, but still, this is barebone hierarchy, as it is populated with more logic it will get slower, so if I can get rid of the dynamic_cast it will be a good thing.
I have verified that for Something s it will be true that &s == static_cast<Node *>(&s) == dynamic_cast<Node *>(&s) but I am not sure whether I can "put all my eggs into that basket" so to speak - is it guaranteed that I can "blindly" reuse the address of every directly or indirectly derived from Node class as a Node * as long, regardless of whether it is created on the stack, Object * s = new Something, Node * s = new Something, Something * s = new Something or any of the many possible variations?
My secondary question is whether this design is a good idea. I do realize that it is somewhat pointless to pass an Object * for parent, considering that only Nodes can be parents, but the Node itself stores its children internally as Object * because not all children derive from Node. Naturally, any recommendations to make this more elegant are welcomed.
If You are worying about this cast, maybe you should define parent member as Node? Then You will never need to cast. Also you will be sure you always use Node as a parent.
Now, for some logical reasons you know that a parent will always be a Node, hence you should use static_cast.
Problem is, it's really hard to debug this code if your assumption fails. What you might want to do is something like this:
#ifdef NDEBUG
Node * p = static_cast<Node*>(parent);
#else
Node * p = dynamic_cast<Node *>(parent);
assert(p);
#endif
p->adopt(this);
This can be easily achieved with boost::polymorphic_downcast.
Node * p = boost::polymorphic_downcast<Node*>(parent);
My professor of languages at university told us to avoid as much as possible dynamic cast and it is something that I try to do every time I have to develop a complex hierarchy.
That being said, if you know exactly that Parent points to a Node then a static cast is safe.
A pointer to member of some class D can be upcast to a pointer to
member of its base class B. This static_cast makes no checks to ensure
the member actually exists in the runtime type of the pointed-to
object.
In short, if you know that D will always be derived from B, using static_cast is safe.
If performance is one of your concerns, I would suggest simplifying (flatten) your hierarchy.
If you know that parent is a Node, then the static_cast does the right thing.
But if you know that parent is a Node, then why not change the constructors (and probably whatever parent pointer you keep inside Object) to reflect this reality?
You can create a fast (O(1), one virtual call) version for your cast if you create a virtual function which transforms object to node if it is a node. In case of succes you get a node* and in case of a failure you get nullptr. This cast is hidden inside the mechanism of the virtual call and thus type safe.
class node;
class object {
// other things
public:
node const* as_node() const { return 0; }
node* as_node() { return 0; }
};
class node {
// other things
public:
node const* as_node() const { return this; }
node* as_node() { return this; }
};
When you do downcast (from Object to Node), you have to use dynamic_cast. For the other way around (Node to Object), static_cast is fine.
The fact that you have to downcast using dynamic_cast indicates a problem in design.
I do not know details, but you could change constructors. Instead of taking Object* parent, you can take Node* parent and Leaf* parent.
So, change :
struct A
{
A( Object* parent );
};
to :
struct A
{
A( Node* parent );
A( Leaf* parent );
};
also, instead of pointers, I would use references.
Another option is to implement the adopt method in all classes. In classes that do not need it, will use default no-op operation, and override it in classes that actually do something. Then you do not need to cast.
class Object {
virtual void foo(){ /*no-op*/ }
};
class Node : public Object {
virtual void foo(){ /* hi-ho */ }
};
class Leaf : public Object {
// uses default foo
};
void callFoo( Object & obj )
{
// no need to cast
obj.foo();
}