Can smart pointers violate the #includes minimization principle in header files?

I prefer to minimize the use #includein my header files using, whenever possible, advanced declarations, and I think this is considered good practice.

It works fine if I have a method declaration, for example:

bool IsFlagSet(MyObject *pObj);

However, if I have typedef Ptr<MyObject> MyObjectPtrin MyObject.h, and the API will change to:

bool IsFlagSet(MyObjectPtr pObj);

Do I really need it now #include "MyObject.h"? Is there a way around this, or is it just the price that pays for using smart pointers?

+5
source share
3 answers

, . , (. 14.3.1/2 ++ 11):

#include <memory>

struct C;

typedef std::shared_ptr<C> ptrC; // C is incomplete here

struct C { void foo() { } };

int main()
{
    ptrC p = std::make_shared<C>();
    p->foo();
}

Pubby, , , , :

struct C;

void foo(C); // C is incomplete here

struct C { };

#include <iostream>

void foo(C)
{
    std::cout << "foo(C)" << std::endl;
}

int main()
{
    C c;
    foo(c);
}
+9

, std::shared_ptr<T> , T . , , , . T , - std::shared_ptr<T>, T*.

+3

typedef .

?

MyClassPtr, , , std::unique_ptr<MyClass> , . .

0
source

All Articles