Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:22:31+00:00 2026-06-17T04:22:31+00:00

How can i forward declare a class in a namespace. For example below is

  • 0

How can i forward declare a class in a namespace. For example below is a header file of a library and user dont need to know about private myPtr so dont need to include boost headers when include below header file. So how can I forward declare the boost::shared_ptr to enable user code compilation?

MyClass.h

class MyClass
{

private:

  boost::shared_ptr<Mytype> myPtr;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:22:33+00:00Added an answer on June 17, 2026 at 4:22 am

    TL;DR The inclusion of <boost/shared_ptr.hpp> is required here. No (intelligent) way around it. MyType itself can be forward-declared though.

    Of course, you can just write #include <boost/shared_ptr.hpp> at the top of your header so your users don’t have to do it themselves. It’s actually good practice to provide autonomous headers (ie headers that can be included first without errors).


    The rules about forward compilation are slightly complicated. It is easier to understand the reason about them than to try and memorize all the cases.

    There are two factors:

    • semantics
    • memory attributes (size & alignment)

    Semantics: in order to access the objects’ methods, attributes or base classes, you need to know about them. Seems obvious, of course, except that the constructors, assignment operators and destructors, even when automatically generated, are methods. It is easy to forget about them.

    Memory attributes: unlike most languages, C++ tries to be as efficient as possible, which means that it will allocate memory for the object right there rather than allocate it somewhere and just use a pointer at the point of use, unless you instruct it to do so of course (by using either pointer or reference). In order to know how much to allocate, the compiler needs to see the guts of the object, ie what’s underneath the hood. This means that even though the exact details are not accessible (private/protected stuff) they need be visible so it can see that 24 bytes aligned on a 8-bytes boundary are required (not relevant to shared_ptr by the way).

    In the Standard we say the object’s definition is required for either of those two needs (methods and memory layout). If the definition is required, then it must be available obviously.


    Okay, now that we know the reasons, we can check various things. Is a definition needed when:

    • using an object as argument to sizeof or alignof? yes (obviously, memory attributes required)
    • using an object as attribute? yes (memory attributes required)
    • using an object as static attribute? no (1)
    • using a pointer or reference to an object as attribute? no (2)
    • using an object as an argument in a function declaration? no (3)
    • using an object as a return type in a function declaration? no (3)
    • passing a pointer or reference to an object around? no (4)
    • casting to base class? yes (semantic check of presence & accessibility of base class)
    • converting to another type? depends (5)

    (1) the declaration does not require anything, however the definition of the static attribute will require the object’s definition.

    (2) a pointer is either 32 bits or 64 bits large (depending on how you compile, …) independently of the object. A reference has an implementation-defined representation.

    (3) even if taken/returned by value! It may be required for the function definition (if used within) or the function call site though.

    (4) of course, should you try to use it (p->foo() or p.foo()) then it’s another story.

    (5) if you need to use the object’s conversion operators, then it’s required obviously; otherwise if you use a constructor of the other type then the same rules as for functions apply (the other type definition is required though).

    I hope things are clearer now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: // can't forward declare with class Foo::Bar // actual class class Foo {
I know that I can do: class Foo; but can I forward declare a
I can forward declare a function in a namespace by doing this: void myNamespace::doThing();
I know that I can forward messages to instances of a class using -forwardInvocation:
In a header file, I have forward declared two members of a namespace: namespace
Possible Duplicates: C++, removing #include<vector> or #include<string> in class header Forward declare an STL
The forward declaration of STL library class std::queue is as follows: namespace std {
Is it possible to forward declare an standard container in a header file? For
I've got some code in which I need to forward-declare the a template class
I can't figure out how to forward declare a windows struct. The definition is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.