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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:45:43+00:00 2026-06-17T10:45:43+00:00

Can I use forward declaration for a class in order to put it’s definition

  • 0

Can I use forward declaration for a class in order to put it’s definition and Implementation later in the program after it’s been used (similar to what is done about functions)?

(I need to join multiple source files of a program into a file, and i want to put the classes’ definitions and Implementations at the end of the file in order to main be at the top of the file.)

  • 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-17T10:45:44+00:00Added an answer on June 17, 2026 at 10:45 am

    Yes you can, to a certain extent.

    You have to realize that the C++ compiler is quite stupid, and doesn’t read ahead. This is the reason why you have to use function prototypes (among some other reasons).
    Now, a function isn’t hard for compiler to resolve. It just looks at the return type of the function, and the types of the parameters of the function, and just assumes that the function is there, without any knowledge about what’s actually inside the function, because it ultimately doesn’t matter at that point.

    However, the contents of the class do matter (the compiler needs to know the size of the class for example). But remember about the not reading ahead bit? When you forward define a class, the compiler doesn’t know about what’s in it, and therefore is missing a lot of information about it. How much space does is need to reserve for example?

    Therefore, you can forward define classes, but you can’t use them as value types. The only thing you can do with it (before it has been concretely declared), is use pointers to it (and use it as a function return type and template argument, as pointer out by
    @Cheersandhth.-Alf).

    If the thing you need to use isn’t a pointer, you should probably use headers (read this if you want to learn more about that).

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

Sidebar

Related Questions

Can I use forward declaration for template class? I try: template<class que_type> class que;
I know it is proper practice to put a forward class declaration (@class ClassA)
I'm pretty clear on when I can/can't use forward declaration but I'm still not
I usually use forward declaration predominantly, if I have a class that does not
I want to use the forward declaration for the ptree class of boost::property_tree .
I'm trying to use forward declarations as much as I can to minimize compilation
How can I use .htaccess to forward a visitor of a specific IP address
I've got a fairly straight forward question. Why can't I use $('a.view').attr('id') (Ref //1
When trying to use an auto_ptr with a type that was declared with forward-declaration,
To use a struct in a function outside of main(), can you use forward

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.