使用static std::unique_ptr和static std::shared_ptr都不行struct IElementAgendaEvents
{//! Called to allow listeners to modify the agenda by adding/removing entries before applying tool operation. Return true if entries added or invalidated.virtual bool _DoModifyAgendaEntries (ElementAgendaP agenda, AgendaOperation, AgendaModify) {return false;}//! Called to allow listeners to copy additional information from source to destination not appropriate to tool operation.virtual void _OnPreCopyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, ElementCopyContextP) {};//! Called before the tool operation is applied to the agenda.virtual void _OnPreModifyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, bool isGroupOperation) {};//! Called after the tool operation is applied to the agenda.virtual void _OnPostModifyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, bool isGroupOperation) {};//! @cond DONTINCLUDEINDOC//! Called to allow custom clipboard formats to be added for the elements in the agenda.virtual void _DoAddDeferredClipboardFormats (ElementAgendaP, AgendaOperation, AgendaModify, GuiDataObject*) {}//! Called to allow listener to participate in element set dynamics. See RedrawGroupInfo for return status meaning.virtual bool _OnRedrawGroupEvent (ElementAgendaCP, AgendaOperation, AgendaModify, RedrawGroupInfo const*) {return false;}virtual bool Dummy1 (void*) {return false;}
//! @endcond
};struct ElementAgendaEvents : public DgnPlatform::IElementAgendaEvents
{static ElementAgendaEvents& GetInstance()
{static std::unique_ptr<ElementAgendaEvents> _Instance = nullptr; if (nullptr == _Instance)_Instance.reset(new ElementAgendaEvents());return *_Instance;
}
//...
};编译错误:
ElementAgendaEvents.obj : fatal error LNK1235: 损坏或无效的 COFF 符号表
仅使用类名生成一个对象struct IElementAgendaEvents
{//! Called to allow listeners to modify the agenda by adding/removing entries before applying tool operation. Return true if entries added or invalidated.virtual bool _DoModifyAgendaEntries (ElementAgendaP agenda, AgendaOperation, AgendaModify) {return false;}//! Called to allow listeners to copy additional information from source to destination not appropriate to tool operation.virtual void _OnPreCopyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, ElementCopyContextP) {};//! Called before the tool operation is applied to the agenda.virtual void _OnPreModifyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, bool isGroupOperation) {};//! Called after the tool operation is applied to the agenda.virtual void _OnPostModifyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, bool isGroupOperation) {};//! @cond DONTINCLUDEINDOC//! Called to allow custom clipboard formats to be added for the elements in the agenda.virtual void _DoAddDeferredClipboardFormats (ElementAgendaP, AgendaOperation, AgendaModify, GuiDataObject*) {}//! Called to allow listener to participate in element set dynamics. See RedrawGroupInfo for return status meaning.virtual bool _OnRedrawGroupEvent (ElementAgendaCP, AgendaOperation, AgendaModify, RedrawGroupInfo const*) {return false;}virtual bool Dummy1 (void*) {return false;}
//! @endcond
};struct ElementAgendaEvents : public DgnPlatform::IElementAgendaEvents
{static ElementAgendaEvents& GetInstance()
{static ElementAgendaEvents obj;return obj;}
//...
};编译成功
使用std::unique_ptr和std::shared_ptr是可以的。
因为如果使用了static,说明这个变量是要被“暴露”在外面的,它虽然在函数内部,但它的名字是和其他函数一样,暴露在外面。struct IElementAgendaEvents
{//! Called to allow listeners to modify the agenda by adding/removing entries before applying tool operation. Return true if entries added or invalidated.virtual bool _DoModifyAgendaEntries (ElementAgendaP agenda, AgendaOperation, AgendaModify) {return false;}//! Called to allow listeners to copy additional information from source to destination not appropriate to tool operation.virtual void _OnPreCopyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, ElementCopyContextP) {};//! Called before the tool operation is applied to the agenda.virtual void _OnPreModifyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, bool isGroupOperation) {};//! Called after the tool operation is applied to the agenda.virtual void _OnPostModifyAgenda (ElementAgendaCP agenda, AgendaOperation, AgendaModify, bool isGroupOperation) {};//! @cond DONTINCLUDEINDOC//! Called to allow custom clipboard formats to be added for the elements in the agenda.virtual void _DoAddDeferredClipboardFormats (ElementAgendaP, AgendaOperation, AgendaModify, GuiDataObject*) {}//! Called to allow listener to participate in element set dynamics. See RedrawGroupInfo for return status meaning.virtual bool _OnRedrawGroupEvent (ElementAgendaCP, AgendaOperation, AgendaModify, RedrawGroupInfo const*) {return false;}virtual bool Dummy1 (void*) {return false;}
//! @endcond
};struct ElementAgendaEvents : public DgnPlatform::IElementAgendaEvents
{static ElementAgendaEvents& GetInstance()
{/*static*/ std::unique_ptr<ElementAgendaEvents> _Instance = nullptr; if (nullptr == _Instance)_Instance.reset(new ElementAgendaEvents());return *_Instance;
}
//...
};编译成功
看到网上有人说这是一个编译器的bug:
有文章解释如下:
LNK1254, LNK1284, and LNK1235 linker errors may occur while compiling a C source file with the /clr compiler option (822329)
|
©2004 Microsoft Corporation. All rights reserved. |