Click here to Skip to main content
15,881,687 members
Articles / Programming Languages / C++

Forward Declaration and a Pointer

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Mar 2013CPOL2 min read 14.7K   3   4
Why forward declaration can cause some troubles with (smart) pointers.

Mister C was a cool guy. C could make amazing things with just ordinary text files. He used to grab a bunch of them and produce magic binary forms. He could make a spinning cube, web server, or even an operating system.

One time he was running through a plain header file. The day was calm and nothing portended a change. Suddenly mister C noticed one interesting line in the file.

C++
class MyType;

"Cool" - he thought - "a forward declared type". After several other rather common statements that looked similar to this:

C++
class Test
{
public:
    Test() { }
    ~Test() { }
    void doAll() { }

He focused on another piece of code:

C++
private:
    std::unique_ptr<MyType> m_myType; 
}

After that line, everything changed for mister C. He was so amazed by the code that all he could do was to output a nasty error message.

Do you know what the message was? Why did he do that?

The Message

In the output window (Visual Studio), there can be something like:

... while compiling class template member function
'void std::default_delete&lt;_Ty&gt;::operator ()(_Ty *) throw() const'

Or in the error list page:

error C2338: can't delete an incomplete type

Reason

Poor mister C had simply no idea how to delete object inside unique pointer. The deletion should happen in the destructor of the class MyTest of course. But since it was in the header file, the problem arose.

Although mister C appreciated authors' suggestion about forward declared type, his policy told him that at that point, there has to be full type definition. Unique pointer required that, to be more specific, its static deleter has to be defined properly.

Solution

Mister C cannot find a proper solution, this is our task. To help him, we can just move implementation of the destructor to some source file where the type MyType will be completely known. Simple as it is.

Another option is to use shared_ptr instead. Although it is a bit counter intuitive, the reason for that is simple. Shared pointer has dynamic deleter object that is selected at runtime, so there will be no errors at compile time.

Note that when we create some object, we need to know full type definition of course.

Look below for more information about improving communication with you and mister C:

This post is just a quick addition to my previous post about Smart Pointer Gotchas.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Poland Poland
Software developer interested in creating great code and passionate about teaching.

Author of C++17 In Detail - a book that will teach you the latest features of C++17!

I have around 11 years of professional experience in C++/Windows/Visual Studio programming. Plus other technologies like: OpenGL, game development, performance optimization.

In 2018 I was awarded by Microsoft as MVP, Developer Technologies.

If you like my articles please subscribe to my weekly C++ blog or just visit www.bfilipek.com.

Comments and Discussions

 
QuestionForward Declaration makes no sense for header only! Pin
pip01015-Feb-16 0:41
pip01015-Feb-16 0:41 
Questionstd::auto_ptr ? Pin
User 740103628-Mar-14 7:08
User 740103628-Mar-14 7:08 
AnswerRe: std::auto_ptr ? Pin
Bartlomiej Filipek30-Mar-14 20:51
Bartlomiej Filipek30-Mar-14 20:51 
QuestionImplementation Hiding with C++11 unique_ptr and shared_ptr Pin
George L. Jackson8-Mar-13 8:39
George L. Jackson8-Mar-13 8:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.