Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smart pointer class predeclaration

Tags:

c++

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?

like image 763
tommyk Avatar asked Mar 26 '26 23:03

tommyk


1 Answers

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.

like image 54
Marcelo Cantos Avatar answered Mar 29 '26 12:03

Marcelo Cantos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!