I have 2 classes: DataObject and DataElement. DataObject holds pointers to (only) DataElements, and a DataElement contains pointers to several types, among which a DataObject.
This used to be no problem, since I only use pointers to DataObjects in DataElement, so a forward declaration of DataObject in the header of DataElement is enough.
Now, however, I try to add a destructor to DataElement, in which I need a delete on a DataObject. On this the compiler says:
dataelement/destructor.cc: In destructor ‘DataElement::~DataElement()’:
dataelement/destructor.cc:8: warning: possible problem detected in invocation of delete operator:
dataelement/destructor.cc:8: warning: invalid use of incomplete type ‘struct DataObject’
dataelement/dataelement.h:7: warning: forward declaration of ‘struct DataObject’
dataelement/destructor.cc:8: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
How can I solve this? A forward declaration is apparently not enough, while I cannot include the complete header for DataObject, since that gives me a circular dependency again.
Thanks in advance!
Define the destructor in a .cpp file that includes both headers.
Make the destructor for the first class defined outside of the class body and after the second class, e.g.
class DataElement;
class DataObject
{
DataElement* elem;
public:
~DataObject();
};
class DataElement
{
DataObject* obj;
public:
~DataElement() { delete obj; }
};
DataObject::~DataObject()
{
delete elem;
}
Related
I have a header with forward-declared classes that I want to declare some smart pointer members in. However, since the smart pointer template types are technically incomplete at time of declaration, I'm getting compilation errors regarding sizeof not being able to be used for unique pointers. Do I have to pull in the implementation headers into my class's header, rather than forward-declaring them, or is there an additional stub I can provide to my forward declaration that will satisfy the smart pointers?
For instance, this is the header file:
#include <memory>
namespace N {
class A;
class Owner {
std::unique_ptr<A> myMember;
}
}
And this is the source file:
#include "A.H"
class Owner{
Owner() {
myMember = std::make_unique<A>("arg1", "arg2");
}
}
I'd rather not bleed dependencies to everyone who includes my header, which is why the classes were originally forward declared when I was using raw pointers.
How can I declare smart pointers in a header file with incomplete forward-declared classes?
By defining the constructor, destructor and other functions that depend on complete definition of the pointed class outside of the header and within a separate translation unit.
Do I have to pull in the implementation headers into my class's header
No. That is only necessary if you don't define the member functions outside of the class definition as I described.
Example:
struct Owner {
std::unique_ptr<A> myMember;
Owner();
~Owner();
Owner(Owner&&);
Owner& operator=(Owner&&);
};
class A {};
Owner::Owner() = default;
Owner::~Owner() = default;
Owner::Owner(Owner&&) = default;
Owner& Owner::operator=(Owner&&) = default;
How can one force the compiler to make the default destructor of a class non-inline?
One way of doing this is to write an empty destructor definition, but it feels messy and also you get a warning from the static analyzer (clang-tidy in my case), that = default should be used for a trivial destructor.
To elaborate more on the actual use case - the goal is to have something similar to:
MyClass.h
class MyClassImpl;
class MyClass {
std::unique_ptr<MyClassImpl> m_impl;
public:
MyClass();
// and some other methods
};
A std::unique_pointer to an incomplete type, which is forward declared in the header and the definition is known only in the source file.
The code above will give a compiler error:
error: use of undefined type 'MyClassImpl'
The actual problem is, that the default destructor of MyClass generated by the compiler is inline and so it needs the complete type info of MyClassImpl.
This can be fixed by adding an empty destructor for MyClass (by declaring in the header and defining in the source file, since defining in the header will implicitly make it inline which will cause the same error).
But is this the only way in modern C++?
Just implement it in cpp file as ordinary method:
MyClass.h
class MyClassImpl;
class MyClass {
std::unique_ptr<MyClassImpl> m_impl;
public:
MyClass();
~MyClass() /*noexcept*/;
// and some other methods
};
in cpp
MyClass::~MyClass() /*noexcept*/ = default;
Is it possible to separate class declaration from its definition? Of course it is, but what if I want to have an object of this class before the actual definition of it? Consider the following example:
class ID3_Reader{
public:
// code omitted for brevity
protected:
class Mp3_File;
Mp3_File mFile;
};
It's obvious that it will not compile. I would have to define the Mp3_File class inside the ID3_Reader class. It's not a problem when I have just two classes. What if there would be like five of them? My code would become pretty messy. To avoid this problem I would have to separate the class declarations from their definitions. How can I achieve that? Remember that I need an instance of the Mp3_File class inside the ID3_Reader class.
The reason why I'm using nested classes is because I don't want some other programmer to use the Mp3_File class. I used "protected" keyword because I will create classes based on the ID3_Reader class.
You can achive this by using a pointer like the other guys answered:
class Mp3_File; // forward declaration of class Mp3_File
class ID3_Reader{
public:
// code omitted for brevity
protected:
Mp3_File *mFile;
};
Or you can declare the constructor of class Mp3_File private and declare class ID3_Reader friend of class Mp3_File:
class Mp3_File {
Mp3_File() {} // constructor is private
friend class ID3_Reader;
};
class ID3_Reader{
public:
// code omitted for brevity
protected:
Mp3_File mFile;
};
auto main() -> int {
ID3_Reader r;
Mp3_File m; // Error Mp3_File constructor is private!
return 0;
}
Thus, other people won't be able to use Mp3_File while you can use it in the scope of class ID3_Reader.
It won't compile because the compiler doesn't know how much memory that Mp3_File class will use. If you change it to a pointer
class ID3_Reader{
public:
// code omitted for brevity
protected:
class Mp3_File;
Mp3_File *mFile;
};
that compiles just fine (a pointer has a fixed size - http://ideone.com/VmmXfK).
I suggest using a pointer instead of a complete member variable and initialize/deinitialize it in the ctor/dtor.
I don't see another way of doing it without changing the "nested classes" design.
You can achieve that by using pointers. Recall that although you must have a full class to define a variable, a simple forward declaration is good to define a pointer:
class ID3_Reader{
public:
// code omitted for brevity
protected:
class Mp3_File;
Mp3_File *mFile;
};
Unfortunately, this puts you on the hook for managing the memory for the nested class, but it does hide all of the internals of the class from outside programmers.
Instead of defining mFile as an instance of Mp3_File, define it as a pointer to an Mp3_File. That way you won't need to know the definition in the header file. Or better yet - use a smart pointer. You will then need to create the real instance by using new in the class constructor and delete it in ID3_Reader's destructor.
If you wish to stay with your current syntax to access mFile from outside of the class, dereference it inside the accessor function:
Mp3_File& getMp3(){ return *mFile; };
Then - if Mp3_File has an overloaded operator() (or any other overloaded operator as a matter of fact), you won't need to dereference it every time manually.
You can make your class a class template to work around this restriction: for class templates the definition of the nested type needs to be visible at instantiation time, not while looking at the definition of the class template. You might want to use a typedef to actually name the used instantiation to avoid the need to have a trailing <>. Here is a quick demo:
template <typename = void>
class ID3_ReaderT {
public:
// code omitted for brevity
protected:
class Mp3_File;
Mp3_File mFile;
};
typedef ID3_Reader<> ID3_Reader;
template <typename T>
class ID3_ReaderT<T>::Mp3_File {
};
int main()
{
ID3_Reader reader;
}
Of course, it still means that every user of ID3_Reader needs to see the definition of the nested type. If you want to avoid that, your option is on level indirection, i.e., using a pointer as was already stated by multiple answers.
I have a basic question that has bothered me for sometime.
When using a Class within a Class I can define the header of the Class I want to use in the header file. I have seen two ways of doing this and would like to know the difference between the two methods?
ex1
#include "ClassA.h"
class ClassB {
public:
ClassB();
~ClassB();
ClassA* a;
};
#endif
ex2 Here is the other way of doing it. The ClassA Header would be defined in ClassB source file.
class ClassA;
class ClassB {
public:
ClassB();
~ClassB();
ClassA* a;
};
#endif
What are the differences with these two methods?
The comlpete layout of the classA is known to the compiler when you include the class definition.
The second syntax is called Forward declaration and now classA is an Incomplete type for the compiler.
For an Incomplete type,
You can:
Declare a member to be a pointer or a reference to the incomplete type.
Declare functions or methods which accepts/return incomplete types.
Define functions or methods which accepts/return pointers/references to the incomplete type (but without using its members)
But You cannot:
Use it as a base class.
Use it to declare a member.
Define functions or methods using this type.
Use its methods or fields, in fact trying to dereference a variable with incomplete type.
So Forward Declaring the class might work faster, because the complier does not have to include the entire code in that header file but it restricts how you can use the type, since it becomes an Incomplete type.
The second method only allows you to create pointers to ClassA, as it's size is unknown. It may however compile faster as the header for the full definition for ClassA is not included.
The latter is a forward declaration. This way you can declare a pointer or reference to a class, even though you have not yet fully declared it. This can be used to resolve cyclic dependencies. What if, in your first example, A also wants to use a pointer to B. This wouldn't work, because when A is declared, B is not known yet. To solve this, you can use a forward declaration to tell the compiler that there is a class B, and you will tell it later what it looks like.
I want to use forward declaration of a class in my software, so I can have typedefs
and use them inside the class full declaration.
Smth like this:
class myclass;
typedef boost::shared_ptr<myclass> pmyclass;
typedef std::list<pmyclass > myclasslist;
class myclass : public baseclass
{
private: // private member declarations
__fastcall myclass();
public: // public member declarations
__fastcall myclass(myclass *Parent)
: mEntry(new myclass2())
{
this->mParent = Parent;
}
const myclass *mParent;
myclasslist mChildren;
boost::scoped_ptr<myclass2> mEntry;
};
so my question is:
are there any drawbacks in this method? I recall some discussion on destructor issues with forward declaration but I did not get everything out of there.
or is there any other option to implement something like this?
Thanks.
EDIT:
I found the discussion I was referring to: here
The main drawback is everything. Forward declarations are a compromise to save compilation time and let you have cyclic dependencies between objects. However, the cost is you can only use the type as references and can't do anything with those references. That means, no inheritance, no passing it as a value, no using any nested type or typedef in that class, etc... Those are all big drawbacks.
The specific destruction problem you are talking about is if you only forward declare a type and happen to only delete it in the module, the behavior is undefined and no error will be thrown.
For instance:
class A;
struct C
{
F(A* a)
{
delete a; // OUCH!
}
}
Microsoft C++ 2008 won't call any destructor and throw the following warning:
warning C4150: deletion of pointer to incomplete type 'A'; no destructor called
: see declaration of 'A'
So you have to stay alert, which should not be a problem if you are treating warnings as errors.
From the C++ standard:
5.3.5/5:
"If the object being deleted has incomplete class type at the point of
deletion and the complete class has a non-trivial destructor or a
deallocation function, the behavior is undefined."