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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:18:14+00:00 2026-05-16T18:18:14+00:00

In a .h if I have: #pragma once #include <xxxx.h> #include yyyy.h class AAAAAA;

  • 0

In a .h if I have:

#pragma once

#include <xxxx.h>
#include "yyyy.h"

class AAAAAA;
class BBBBBB;

class ZZZZZZ
{
     public:

     // etc
 };

using class AAAAAA; is forward declaring a class right?

Why would one do this?

Is it needed?

What are the benefits? Drawbacks?

  • 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-05-16T18:18:15+00:00Added an answer on May 16, 2026 at 6:18 pm

    Why would one do this?

    Because the compiler only knows names that have been declared. So if you want to use a class, you have to declare it. But if its definition depends on its user, a forward declaration can suffice if the user doesn’t depend on the definitin of the class in turn, but just on its declaration (= name).

    In particular, if the user just needs a pointer or referece, it doesn’t depend on the definition. But in the cases listed (which do not claim to be exclusive, since it’s a standard non-normative excerpt), the user depends on the definition of class T if

    • an object of type T is defined (3.1), or
    • T is used as the object type or array element type in a new-expression (5.3.4), or
    • an lvalue-to-rvalue conversion is applied to a lvalue referring to an object of type T (4.1), or
    • an expression is converted (either implicitly or explicitly) to type T (Clause 4, 5.2.3, 5.2.7, 5.2.9, 5.4),
      or
    • an expression that is not a null pointer constant, and has type other than void*, is converted to the
      type pointer to T or reference to T using an implicit conversion (Clause 4), a dynamic_cast (5.2.7) or
      a static_cast (5.2.9), or
    • a class member access operator is applied to an expression of type T (5.2.5), or
    • the typeid operator (5.2.8) or the sizeof operator (5.3.3) is applied to an operand of type T, or
    • a function with a return type or argument type of type T is defined (3.1) or called (5.2.2), or
    • a class with a base class of type T is defined (Clause 10), or
    • an lvalue of type T is assigned to (5.17), or
    • an exception-declaration has type T, reference to T, or pointer to T (15.3).

    In these cases, a forward declaration will not suffice and you need to fully define it.

    Is it needed?

    Yes, in the cases where it is needed it is needed. In the following case it is, because both classes refer to each other.

    class A;
    
    class B {
      // forward declaration needed
      void f(A);
    };
    
    class A {
      void f(B);
    };
    
    // "- a function with a return type or argument type of type T is defined"
    void B::f(A) {
    
    }
    

    The member function definition required not only a declaration but also the definition of class A.

    What are the benefits? Drawbacks?

    The benefits are that your program compiles. The drawback is that you have polluted the scope with just another name. But that’s the necessary evil of it.

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

Sidebar

Related Questions

I have the following C++ code: #pragma once #include StdAfx.h #include <string> using namespace
I have a header file like so: #pragma once #include gamestate.h #include ExitListener.h class
I have the following class: #pragma once #include <string> #include <iostream> class testclass {
I have the following code in a header only file. #pragma once class error_code
I have a header defined as follows header #pragma once #include <map> #include <string>
#pragma once #include Player.h class Player; //class SmallHealth; const int kNumOfCards = 3; //for
I have a simple C++ base class, derived class example. // Base.hpp #pragma once
I have tried all the solutions that have been provided including using PRAGMA but
I can't link properly to glew. I have done: #define GLEW_STATIC #include glew/glew.h #pragma
I have an Assembly(A) which defines a Managed class which has a public constructor

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.