Does a QSharedPointer have to include headers rather than a forward declaration ?
-
Before I use QSharedPointer, I am used to use forward declaration to declare my class instead of include its .h in my header file code like :
class MyClass; class Controller { private: MyClass* myClassp; };
But when I start using QSharedPointer, and I have to use the traditional way, which means u have to include its .h file like:
#include "myclass.h" class Controller { private: QSharedPointer<MyClass> myClass; };
Can I use the forward declaration and QSharedPointer at a same time ? Due to I want the convience of both way.
-
unless the meta system does something weird with the Qt implementation of shared pointers, it should work. Forward declarations are an integral part of C++.
Shared pointers are usually declared in a function scope so that they automatically delete the object. Your use of a shared pointer as a class variable is, IMHO, not the right way. If you are declaring it as a class variable then just created the object in the contstructor and delete it in the destructor. No need for a private shared pointer as a class variable.
-
Before I use QSharedPointer, I am used to use forward declaration to declare my class instead of include its .h in my header file code like :
class MyClass; class Controller { private: MyClass* myClassp; };
But when I start using QSharedPointer, and I have to use the traditional way, which means u have to include its .h file like:
#include "myclass.h" class Controller { private: QSharedPointer<MyClass> myClass; };
Can I use the forward declaration and QSharedPointer at a same time ? Due to I want the convience of both way.
@MartinChan3 Forward declarations only work as long as you only use your class to declare a pointer. To declare a pointer compiler does not have to know anything about the class as a pointer is always the same thing. But if you use the class itself the compiler needs to know what this class is, so you need to include header file.
-
@MartinChan3 said in Does a QSharedPointer have to include headers rather than a forward declaration ?:
But when I start using QSharedPointer, and I have to use the traditional way
What error message do you get? Since you don't have a ctor (in your example) of the class, the compiler generates one in the header. And since there the dtor of myClass must be called, myClass must be visible.
In the first example without the shared pointer you most likely leak myClasss.
-
unless the meta system does something weird with the Qt implementation of shared pointers, it should work. Forward declarations are an integral part of C++.
Shared pointers are usually declared in a function scope so that they automatically delete the object. Your use of a shared pointer as a class variable is, IMHO, not the right way. If you are declaring it as a class variable then just created the object in the contstructor and delete it in the destructor. No need for a private shared pointer as a class variable.
@Kent-Dorfman Thx,I will avoid using QSharedPointer in member variables of my class. And thanks all above.
-
@MartinChan3 said in Does a QSharedPointer have to include headers rather than a forward declaration ?:
I will avoid using QSharedPointer in member variables of my class.
Why?
-
Before I use QSharedPointer, I am used to use forward declaration to declare my class instead of include its .h in my header file code like :
class MyClass; class Controller { private: MyClass* myClassp; };
But when I start using QSharedPointer, and I have to use the traditional way, which means u have to include its .h file like:
#include "myclass.h" class Controller { private: QSharedPointer<MyClass> myClass; };
Can I use the forward declaration and QSharedPointer at a same time ? Due to I want the convience of both way.
@MartinChan3 said in Does a QSharedPointer have to include headers rather than a forward declaration ?:
Can I use the forward declaration and QSharedPointer at a same time ? Due to I want the convience of both way.
Define the big four for your class (the one with the
QSharedPointer
member) and it's going to be fine.