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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:22:19+00:00 2026-05-12T00:22:19+00:00

If I have a class in outside.h like: class Outside { public: Outside(int count);

  • 0

If I have a class in outside.h like:

class Outside
{
   public:
       Outside(int count);
       GetCount();
}

How can I use it in framework.cpp using the extern keyword, where I need to instantiate the class and call GetCount?


Edit:

#include is not allowed.

  • 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-12T00:22:19+00:00Added an answer on May 12, 2026 at 12:22 am

    Just to clarify. It is impossible to extern the class:

    class Outside
    {
        public:
            Outside(int count);
            GetCount();
    }
    

    But, once you have the class available in framework.cpp, you CAN extern an object of type Outside. You’ll need a .cpp file declaring that variable:

    #include "outside.h"
    
    Outside outside(5);
    

    And then you can refer to that object in another file via extern (as long as you link in the correct object file when you compile your project):

    #include "outside.h"
    
    extern Outside outside;
    int current_count = outside.GetCount();
    

    extern is used to say "I KNOW a variable of this type with this name will exist when this program runs, and I want to use it." It works with variables/objects, not classes, structs, unions, typedefs, etc. It’s not much different from static objects.

    You may be thinking about forward declaring classes to cut down on compile times, but there are restrictions on that (you only get to use the objects as opaque pointers and are not able to call methods on them).

    You may also mean to hide the implementation of Outside from users. In order to do that, you’re going to want to read up on the PIMPL pattern.


    Update

    One possibility would be to add a free function to Outside.h (I’ve also added a namespace):

    namespace X {
        class Outside {
            int count_;
            public:
                Outside(int count) : count_(count) { }
                int GetCount()
                {
                    return count_;
                }
        };
    
        int GetOutsideCount(Outside* o);
    }
    

    Implement that function in a .cpp file. While you’re at it, you might as well make the global variable that you intend to extern (note, the variable itself does not need to be a pointer):

    #include "outside.h"
    
    namespace X {
        int GetOutsideCount(Outside* o)
        {
            return o->GetCount();
        }
    }
    
    X::Outside outside(5);
    

    And then do this in your program (note that you cannot call any methods on outside because you did not include outside.h and you don’t want to violate the one definition rule by adding a new definition of the class or those methods; but since the definitions are unavailable you’ll need to pass pointers to outside around and not outside itself):

    namespace X {
        class Outside;
        int GetOutsideCount(Outside* o);
    }
    
    extern X::Outside outside;
    
    int main()
    {
        int current_count = GetOutsideCount(&outside);
    }
    

    I consider this an abomination, to put it mildly. Your program will find the GetOutsideCount function, call it by passing it an Outside*. Outside::GetCount is actually compiled to a normal function that takes a secret Outside object (inside Outside::GetCount that object is referred to via the this pointer), so GetOutsideCount will find that function, and tell it to dereference the Outside* that was passed to GetOutsideCount. I think that’s called "going the long way ’round."

    But it is what it is.

    If you aren’t married to using the extern keyword, you can instead go full "let’s use C++ like it’s C" mode by adding the following two functions in the same way (i.e., via forward declarations and implementing right next to int GetOUtsideCount():

    Outside* CreateOutsidePointer(int count)
    {
        return new Outside(count);
    }
    
    void DestroyOutsidePointer(Outside* o)
    {
        return delete o;
    }
    

    I’m more willing to swallow that. It’s a lot like the strategy used by the APR.

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

Sidebar

Related Questions

Say I have a .NET class like so: public class Person { public string
I have class method that returns a list of employees that I can iterate
I have class A: public class ClassA<T> Class B derives from A: public class
If I have class ObjA { public ObjB B; } class ObjB { public
If I have class names such as left, right, clear and xhtml like <a
I have a class A that implements IEquatable<>, using its fields (say, A.b and
consider the following simplified example: public class Ticket { public int Id; public TicketState
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I have class with a member function that takes a default argument. struct Class

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.