tommyk
tommyk

Reputation: 3307

Smart pointer class predeclaration

I have a header file:

class A
{
public:
    DeviceProxyPtr GetDeviceProxy(); 
};

DeviceProxyPtr is defined in a different header file like this:

typedef SmartPtrC<DeviceProxyC> DeviceProxyPtr;

I don't want to include DeviceProxyPtr definition header. If a return type was DeviceProxy* I could simply use predeclaration class DeviceProxy. Is there any way to do the same with my smart pointer class?

Upvotes: 3

Views: 594

Answers (1)

Marcelo Cantos
Marcelo Cantos

Reputation: 185852

The fact that it's a concrete return type makes no difference. You can forward-declare return types.

However, in this case, it's not a class, but a typedef. You couldn't use class DeviceProxy, even if it was a pointer.

Mind you, all hope is not lost. The point of forward declarations is to avoid dragging in too much code and slowing down the compiler. The standard iostream library has exactly the same problem. For instance, istream isn't actually a class, but a typedef of a basic_istream instantiation. The standard library solves this by providing an <iosfwd> header that forward-declares the basic_istream class template and then uses it to declare the istream typedef. Thus, classes that interact with the iostream need only #include <iosfwd> in the header file, and then #include <iostream> in the implementation file.

Upvotes: 2

Related Questions