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

  • Home
  • SEARCH
  • 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 78175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:56:45+00:00 2026-05-10T20:56:45+00:00

I usually, almost without thinking anymore, use forward declarations so that I won’t have

  • 0

I usually, almost without thinking anymore, use forward declarations so that I won’t have to include headers. Something along this example:

//----------------------- // foo.h //----------------------- class foo {    foo();    ~foo(); };   //----------------------- // bar.h //-----------------------  class foo; // forward declaration  class bar {    bar();    ~bar();     foo* foo_pointer; }; 

Some developers like to use this method to avoid problems with inclusion circles. I rather use it to minimize the overhead in extensive inclusion hierarchies, an important part of physical design (for larger projects in particular).

However, in some cases I really like to declare members as normal objects instead of pointers to benefit from the automatic construction/destruction mechanism. This leads to the problem that forward declarations can’t be used anymore, since the compiler needs the class definition in such case, eg:

//----------------------- // foo.h //----------------------- class foo {    foo();    ~foo(); };   //----------------------- // bar.h //-----------------------  class foo;       // Not enough given the way we declare 'foo_object'.. #include 'foo.h' // ..instead this is required  class bar {    bar();    ~bar();     foo foo_object; }; 

So, I would be happy if anyone knows an alternative language construct which can be used here so that I can declare ‘foo_object’ as shown in the example, but without including its header.

Regards

/Robert

  • 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. 2026-05-10T20:56:46+00:00Added an answer on May 10, 2026 at 8:56 pm

    Just use a smart pointer – you can even use auto_ptr in this case.

    //----------------------- // bar.h //-----------------------  #include <memory> class foo;       // Not enough given the way we declare 'foo_object'..  class bar { public:    bar();    ~bar();     foo &foo_object() { return *foo_ptr; }    const foo &foo_object() const { return *foo_ptr; }  private:    auto_ptr<foo> foo_ptr; }; 

    You get all the benefits of automatic memory management, without having to know anything about foo in bar.h. See Wrapping Pointer Data Members for Herb Sutter’s recommendation.

    If you really want default construction to happen automatically, try this:

    #include <iostream> using namespace std;  class Foo;  template <typename T> class DefaultConstuctorPtr {     T *ptr;     void operator =(const DefaultConstuctorPtr &);     DefaultConstuctorPtr(const DefaultConstuctorPtr &);  public:     DefaultConstuctorPtr() : ptr(new T()) {}     ~DefaultConstuctorPtr() { delete ptr; }      T *operator *() { return ptr; }     const T *operator *() const { return ptr; } };  class Bar {     DefaultConstuctorPtr<Foo> foo_ptr; public:     Bar() {} // The compiler should really need Foo() to be defined here? };  class Foo { public:     Foo () { cout << 'Constructing foo'; } };  int main() {     Bar bar; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes C++ solves the problem through something called move semantics.… May 11, 2026 at 8:27 pm
  • Editorial Team
    Editorial Team added an answer It's generally a good idea to avoid this kind of… May 11, 2026 at 8:27 pm
  • Editorial Team
    Editorial Team added an answer Take a look at position(): var pos = $("#myimage").position(); //… May 11, 2026 at 8:27 pm

Related Questions

I usually, almost without thinking anymore, use forward declarations so that I won't have
I almost feel embarrassed to ask, but I always struggle with how to organize
I am using Python to read in data in a user-unfriendly format and transform
When faced with a problem in software I usually see a solution right away.
Why is it that advertised disk space is almost always higher than the disk

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.