extern必须能够访问类构造函数吗?

时间:2012-06-18 22:06:49

标签: c++ class pointers constructor extern

众所周知,有很多方法可以访问班级成员,我现在的问题是。如果类构造函数/解构函数是公共的,那么'new'用法是允许的,也是'extern',如果它是私有的,我可以使'GetInstance'只允许不允许'new'使用的类,这对于类很有用应该只有1个指向实例(例如,计算当前登录用户的服务器),而“new”适用于指向许多对象的classe(例如指向新对象的类,例如a新玩家记录int,它会创建一个指向它们每个on的新指针),而map会存储指向该对象的'new'的指针。问题是,不应该允许'extern'从全局对象私有构造函数访问吗?既然允许'新'使用?看一下下面的例子:

#include <windows.h>
#include <cstdlib>
#include <iostream>
#include <map>

using namespace std;


//CMover.h-------------------------------------
#define MAX_MOVER_NAME 32

class CMover
{
private:
    BOOL m_bInitialized;
    BOOL m_bIsWalking;
    unsigned m_uMetersPercused;
    TCHAR m_szName[MAX_MOVER_NAME+1];

    //CMover( LPCTSTR szMoverName, BOOL bInitialized = TRUE, BOOL bWalking = TRUE, unsigned uMeters = 0 );

public:
        CMover() { };
    virtual ~CMover(){};
};
//---------------------------------------------



//CMover.cpp---------------
CMover g_Mover; //CMover was created in order to have many 'new' usage, so each 'new' object points to a new player
// Making a global object of it is a big failure
//---------------------------


//CServer.h---------------
class CConnectedUsers
{
private:
    CConnectedUsers() {}; //ok, new cannot access, this class should be used as 1 object only
    virtual ~CConnectedUsers() {}; //to count up connected users, 'new' should never be used
public:
    map<u_long,CMover*>m_UserMng;

    //I Could use GetInstance, that any pointers craeted (CConnectedUsers *pCUser = CConnectedUsers::GetInstance() ) would
    //point to it
    static CConnectedUsers* GetInstance( void )
    {
            static CConnectedUsers mObj;
            return &mObj;
    }
};
//------------------------

//CServer.cpp ------
//Or in this case i would like to make a global object, so on CWhatever.cpp that included CServer.h i could use
//(extern CConnectedUsers g_Users;) which is also 1 object-only so no GetInstance would be needed and I would use
//(g_Users.m_UserMng...) directly after external declared
//PROBLEM is, making constructor private regulates the class to be 1 object only, but it doesn't allow external usage
//why is that???
CConnectedUsers g_Users;
//-----------------

//Main.cpp ...etcc

int main( int argc, char *argv[] )
{
    CMover *pMover = new CMover;
    cout << pMover << endl << &pMover << endl; //points to a new object, pointer stored in a region

    CMover *pMov2 = new CMover;
    cout << pMov2 << endl << &pMov2 << endl << endl; //points to a new object, pointer stored in another region

    CConnectedUsers *pCUser = CConnectedUsers::GetInstance();
    CConnectedUsers *pCUser2 = CConnectedUsers::GetInstance();
    cout << pCUser << endl << &pCUser << endl; //points to CConnectedUsers, pointer stored in a region
        cout << pCUser2 << endl << &pCUser2 << endl; //points to same CConnectedUsers, pointer stored in another region

    //also another question is, do I need to check these pointers integrity by doing:
            if( pCUser )
            {
                //??
            }

    system("pause>nul");
    return EXIT_SUCCESS;
}

1 个答案:

答案 0 :(得分:5)

问题不在于extern。带extern的声明不会创建对象,也不需要访问构造函数。

但是,extern引用必须找到在某处定义的对象,并且一个定义(不仅仅是声明)不能访问构造函数,因为它是命名空间成员,而不是类成员。 / p>

这是完全预期和一致的。在命名空间范围内评估的表达式在类之外,因此对私有成员没有特殊权限。

您可以使用“锁定和密钥”系统使公共构造函数不能从任何其他文件中使用。例如:

// header file
class Once
{
public:
    struct Forward;
    Once(Forward);
};

extern Once g_singleton;

// implementation file
#include "Once"
namespace { struct Local {}; }
struct Once::Forward : Local {};

Once g_singleton(Once::Forward());

不可能从任何其他编译单元实例化该类,因为没有其他编译单元具有Once::Forward的定义,该定义必须通过值传递给构造函数。任何在任何其他编译单元中定义Once::Forward的尝试都将成为ODR违规行为。