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.
solved How can I use a class from a header file in a source file using extern but not #include?