I’m wondering if I can call a static member function inside a cpp file like the following:
A.h
class A
{
public:
A(){}
virtual ~A(){}
static void Initialize(){ g_pSomeType = new SomeType();}
private:
static SomeType* g_pSomeType;
};
A.cpp
#include "A.h"
SomeType* A::g_pSomeType = nullptr;
A::Initialize(); //here I get an error: "Initialize may not be redeclared outside of it's class"
Is it possible to do something like this? So that I would be able to initialize the static members inside the .cpp file of the class, to avoid users from having to call initialize() first? I suppose I could find a way to work around this problem but I’m curious if there’s a way to let this work. Thanks.
You cannot write any executable code outside a function body. You can only call
A::Initialize()(or do any other function/method call) only if you’re inside a function or a method…The compiler thinks that you’re declaring
A::Initialize()again rather than calling it.