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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:03:08+00:00 2026-05-23T10:03:08+00:00

Is it possible to create an object in file A.cpp if its class is

  • 0

Is it possible to create an object in file A.cpp if its class is defined in file B.cpp?

What I mean is, you can use extern to access a variable initialized in another file. Is there anything similar for classes?

  • 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-23T10:03:09+00:00Added an answer on May 23, 2026 at 10:03 am

    No. The class definition must be visible to the compiler in the current translation unit if you actually instantiate/use that class.

    Usually you have the definition of the class in a header file that will be included in every .cpp that needs to use that class. Notice that usually methods inside a class definition are only declared, since their implementation (definition) is usually put in a separate .cpp file (unless you have inline methods, that are defined inside the class definition).

    Notice however that you can get away with just a class declaration (usually called forward declaration) if all you need is to declare/define pointers to the class – i.e. if all the compiler needs to know is that a type with that name will be defined later before you actually need to do something on it (instantiate the class, call its methods, …). Again, this is not enough to define a variable/member of the type of the class, because the compiler has to know at the very least the size of the class to decide the memory layout of the other class/of the stack.

    To recap about the terminology and about what you can/cannot do:

    // Class declaration ("forward declaration")
    class MyClass;
    
    // I can do this:
    class AnotherClass
    {
    public:
        // All the compiler needs to know here is that there's some type named MyClass
        MyClass * ptr; 
    };
    // (which, by the way, lets us use the PIMPL idiom)
    
    // I *cannot* do this:
    
    class YetAnotherClass
    {
    public:
        // Compilation error
        // The compiler knows nothing about MyClass, while it would need to know its
        // size and if it has a default constructor
        MyClass instance;    
    };
    
    // Class definition (this can cohexist with the previous declaration)
    class MyClass
    {
    private:
        int aMember;    // data member definition
    public:
        void AMethod(); // method declaration
    
        void AnInlineMethod() // implicitly inline method definition
        {
            aMember=10;
        }
    };
    
    // now you can do whatever you want with MyClass, since it's well-defined
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to use PHP's SimpleXML functions to create an XML object from
Is it possible in Actionscript 3 to create a weak reference to an object,
Is it possible to create Selenium tests using the Firefox plugin that use randomly
It's possible to create digrams in SQL Server 2000 that can be useful to
In Java, is it possible to create a class definition on the fly while
Having a object x which is an instance of some class how to create
I'm trying to create a simple class to read a csv file and store
Is it possible for the CLR to create Object X that inherits from Object
Is it possible to create a NumPy object that behaves very much like a
Is it possible to create federated Subversion servers? As in one server at location

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.