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 7851131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:05:33+00:00 2026-06-02T19:05:33+00:00

I usually use forward declaration predominantly, if I have a class that does not

  • 0

I usually use forward declaration predominantly, if I have a class that does not need complete definition in .hpp file

Ex)

 //B.hpp

 namespace A_file {
   class A;
 }

 namespace B_file {

  class B {
   public:
        B();
   private:
        A *ptr_to_A;
  }
 }

 //B.cpp

 #include "A.hpp"
 using namespace A_file;

 namespace B_file {

   B(int value_) {
        *ptr_to_A = new A(value_);
   }

   int some_func() {
        ptr_to_A->some_func_in_A();
   }
 }

I write this kind of code. I think, it will save including the whole hpp again. (Feel free to comment, if you thing, this is not healthy)

Is there a way that I can do the same for objects/classes in std namespace?
If there is a way, is it okay or does it have side effects?

  • 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-02T19:05:34+00:00Added an answer on June 2, 2026 at 7:05 pm

    You can forward declare your own classes in header files to save compilation time. But you can’t for classes in namespace std. According to the C++11 standard, 17.6.4.2.1:

    The behavior of a C++ program is undefined if it adds declarations or
    definitions to namespace std or to a namespace within namespace std
    unless otherwise specified.

    Note that some of these classes are typedefs of class templates, so a simple forward declaration will not work. You can use #include<iosfwd> instead of #include<iostream> for example, but there are no similar headers with just forward declarations for string, vector, etc.

    See GotW #34, Forward Declarations for more information.

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

Sidebar

Related Questions

I usually, almost without thinking anymore, use forward declarations so that I won't have
I usually use this line to import file from out of the current folder
I usually use ax.set_rasterized(True) to rasterize the figure so that it could handle transparency
I usually use sql parameters with queries, but in this case I need to
I usually use expressions like this CommandArgument='<%# Container.DataItemIndex.ToString() %> ' But I could not
Is it possible to forward declare a class that uses default arguments without specifying
I usually use the following Python code to read lines from a file :
I usually use one preference XML file to store all app-wide data. Now I
I usually use IIS in conjunction with the OutSystems development platform, which does code-generation
I usually use the UUID class to generate unique IDs. This works fine if

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.