我如何在 C++ 中调用抽象基类的函数?

发布于 2024-11-18 20:48:30 字数 6321 浏览 8 评论 0原文

我正在使用 VSS API 并使用这个抽象基类 CVSSWriter。我想调用它的函数,但我无法创建它的对象。我应该如何继承它,因为它是 VSWriter.h 中定义的类 这是有关其方法的信息的链接 http://msdn.microsoft.com/en -us/library/aa381524%28v=VS.85%29.aspx

请帮助我,因为我陷入困境。

我尝试过这个,但它不起作用。

class SPCVssWriterEx:public CVssWriter
{
private:
   LONG _cRef;


public:
      SPCVssWriterEx(){}

     ~SPCVssWriterEx(){}

 // callback for prepare snapsot event
 bool STDMETHODCALLTYPE OnPrepareSnapshot()
     {
      return true;
     }


 // callback for freeze event
 bool STDMETHODCALLTYPE OnFreeze()
     {
       return true;
     }


 // callback for thaw event
 bool STDMETHODCALLTYPE OnThaw()
     {
       return true;
     }

// callback if current sequence is aborted
 bool STDMETHODCALLTYPE OnAbort()
    {
      return true;
    }

};

这四个是纯虚函数,还有一些虚函数。

该类的实现是

'

    class CVssWriter
   {
     // Constants
     public:

   // Constructors & Destructors
    public:
__declspec(dllexport)
STDMETHODCALLTYPE CVssWriter();

__declspec(dllexport)
virtual STDMETHODCALLTYPE ~CVssWriter();

    // Exposed operations
    public:
// initialize the writer object
__declspec(dllexport)
HRESULT STDMETHODCALLTYPE Initialize
    (
    IN VSS_ID WriterID,
    IN LPCWSTR wszWriterName,
    IN VSS_USAGE_TYPE ut,
    IN VSS_SOURCE_TYPE st,
    IN VSS_APPLICATION_LEVEL nLevel = VSS_APP_FRONT_END,
    IN DWORD dwTimeoutFreeze = 60000,           // Maximum milliseconds between Freeze/Thaw
    IN VSS_ALTERNATE_WRITER_STATE aws = VSS_AWS_NO_ALTERNATE_WRITER,
    IN bool bIOThrottlingOnly = false,
    IN LPCWSTR wszWriterInstanceName = NULL
    );

// cause the writer to subscribe to events.
__declspec(dllexport)
HRESULT STDMETHODCALLTYPE Subscribe
    (
    IN DWORD dwEventFlags = VSS_SM_BACKUP_EVENTS_FLAG | VSS_SM_RESTORE_EVENTS_FLAG
    );

// cause the writer to unsubscribe from events
__declspec(dllexport)
HRESULT STDMETHODCALLTYPE Unsubscribe();

    // installs an alternative writer
   __declspec(dllexport)
    HRESULT STDMETHODCALLTYPE InstallAlternateWriter
    (
    IN VSS_ID writerId,
    IN CLSID persistentWriterClassId
    );

// Internal properties - accessible from OnXXX methods
    protected:

// get array of volume names
__declspec(dllexport)
LPCWSTR* STDMETHODCALLTYPE GetCurrentVolumeArray() const;

// get count of volume names in array
__declspec(dllexport)
UINT STDMETHODCALLTYPE GetCurrentVolumeCount() const;

// get the name of the snapshot device corresponding to a given volume.
__declspec(dllexport)
HRESULT STDMETHODCALLTYPE GetSnapshotDeviceName
  (
  IN LPCWSTR wszOriginalVolume,
  OUT LPCWSTR* ppwszSnapshotDevice
  ) const;

// current snapshot set GUID
__declspec(dllexport)
VSS_ID STDMETHODCALLTYPE GetCurrentSnapshotSetId() const;

// Current backup context.  
__declspec(dllexport)
LONG STDMETHODCALLTYPE GetContext() const;

// current app level (either 1,2,3)
__declspec(dllexport)
VSS_APPLICATION_LEVEL STDMETHODCALLTYPE GetCurrentLevel() const;

// determine if path is in set of volumes being snapshotted
__declspec(dllexport)
bool STDMETHODCALLTYPE IsPathAffected
    (
    IN LPCWSTR wszPath
    ) const;

// does the backup include bootable state (formerly system state backup)
__declspec(dllexport)
bool STDMETHODCALLTYPE IsBootableSystemStateBackedUp() const;

// is the backup application smart (i.e., selecting components) or
// dump (i.e., just selecting volumes)
__declspec(dllexport)
bool STDMETHODCALLTYPE AreComponentsSelected() const;

__declspec(dllexport)
VSS_BACKUP_TYPE STDMETHODCALLTYPE GetBackupType() const;

__declspec(dllexport)
VSS_RESTORE_TYPE STDMETHODCALLTYPE GetRestoreType() const;

__declspec(dllexport)
bool STDMETHODCALLTYPE IsPartialFileSupportEnabled() const;

_declspec(dllexport)
    HRESULT STDMETHODCALLTYPE SetWriterFailure(HRESULT hr);

    // Ovverides
    public:
// callback when request for metadata comes in
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnIdentify(IN IVssCreateWriterMetadata *pMetadata);

// callback for prepare backup event
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnPrepareBackup(
    IN IVssWriterComponents *pComponent
    );

// callback for prepare snapsot event
virtual bool STDMETHODCALLTYPE OnPrepareSnapshot() = 0;

// callback for freeze event
virtual bool STDMETHODCALLTYPE OnFreeze() = 0;

// callback for thaw event
virtual bool STDMETHODCALLTYPE OnThaw() = 0;

// callback if current sequence is aborted
virtual bool STDMETHODCALLTYPE OnAbort() = 0;

// callback on backup complete event
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnBackupComplete
    (
    IN IVssWriterComponents *pComponent
    );

// callback indicating that the backup process has either completed or has shut down
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnBackupShutdown
    (
    IN VSS_ID SnapshotSetId
    );

// callback on pre-restore event
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnPreRestore
    (
    IN IVssWriterComponents *pComponent
    );

// callback on post-restore event
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnPostRestore
    (
    IN IVssWriterComponents *pComponent
    );

// callback on post snapshot event
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnPostSnapshot
    (
    IN IVssWriterComponents *pComponent
    );

// callback on back off I/O volume event
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnBackOffIOOnVolume
    (
    IN VSS_PWSZ wszVolumeName,
    IN VSS_ID snapshotId,
    IN VSS_ID providerId
    );

// callback on Continue I/O on volume event
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnContinueIOOnVolume
    (
    IN VSS_PWSZ wszVolumeName,
    IN VSS_ID snapshotId,
    IN VSS_ID providerId
    );

// callback to specify that the volume snaphost service is shutting down.  Used
// by alternative writers to signal when to shutdown.
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnVSSShutdown();

// callback to an alternative writer when the application writer subscribes.  Used to
// signal the alternative writer to shutdown.
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnVSSApplicationStartup();

private:

IVssWriterImpl *m_pWrapper;
};

'

I am using VSS API and using this abstract base class CVSSWriter.I want to call its functions but I can't make its object. How should I inherit it because its a class defined in VSWriter.h
Here is the link for information about its methods
http://msdn.microsoft.com/en-us/library/aa381524%28v=VS.85%29.aspx

Please help as I am stuck.

I tried this but it does not work.

class SPCVssWriterEx:public CVssWriter
{
private:
   LONG _cRef;


public:
      SPCVssWriterEx(){}

     ~SPCVssWriterEx(){}

 // callback for prepare snapsot event
 bool STDMETHODCALLTYPE OnPrepareSnapshot()
     {
      return true;
     }


 // callback for freeze event
 bool STDMETHODCALLTYPE OnFreeze()
     {
       return true;
     }


 // callback for thaw event
 bool STDMETHODCALLTYPE OnThaw()
     {
       return true;
     }

// callback if current sequence is aborted
 bool STDMETHODCALLTYPE OnAbort()
    {
      return true;
    }

};

These four are the pure virtual functions and there are some virtual functions as well.

The implementation for that class is

'

    class CVssWriter
   {
     // Constants
     public:

   // Constructors & Destructors
    public:
__declspec(dllexport)
STDMETHODCALLTYPE CVssWriter();

__declspec(dllexport)
virtual STDMETHODCALLTYPE ~CVssWriter();

    // Exposed operations
    public:
// initialize the writer object
__declspec(dllexport)
HRESULT STDMETHODCALLTYPE Initialize
    (
    IN VSS_ID WriterID,
    IN LPCWSTR wszWriterName,
    IN VSS_USAGE_TYPE ut,
    IN VSS_SOURCE_TYPE st,
    IN VSS_APPLICATION_LEVEL nLevel = VSS_APP_FRONT_END,
    IN DWORD dwTimeoutFreeze = 60000,           // Maximum milliseconds between Freeze/Thaw
    IN VSS_ALTERNATE_WRITER_STATE aws = VSS_AWS_NO_ALTERNATE_WRITER,
    IN bool bIOThrottlingOnly = false,
    IN LPCWSTR wszWriterInstanceName = NULL
    );

// cause the writer to subscribe to events.
__declspec(dllexport)
HRESULT STDMETHODCALLTYPE Subscribe
    (
    IN DWORD dwEventFlags = VSS_SM_BACKUP_EVENTS_FLAG | VSS_SM_RESTORE_EVENTS_FLAG
    );

// cause the writer to unsubscribe from events
__declspec(dllexport)
HRESULT STDMETHODCALLTYPE Unsubscribe();

    // installs an alternative writer
   __declspec(dllexport)
    HRESULT STDMETHODCALLTYPE InstallAlternateWriter
    (
    IN VSS_ID writerId,
    IN CLSID persistentWriterClassId
    );

// Internal properties - accessible from OnXXX methods
    protected:

// get array of volume names
__declspec(dllexport)
LPCWSTR* STDMETHODCALLTYPE GetCurrentVolumeArray() const;

// get count of volume names in array
__declspec(dllexport)
UINT STDMETHODCALLTYPE GetCurrentVolumeCount() const;

// get the name of the snapshot device corresponding to a given volume.
__declspec(dllexport)
HRESULT STDMETHODCALLTYPE GetSnapshotDeviceName
  (
  IN LPCWSTR wszOriginalVolume,
  OUT LPCWSTR* ppwszSnapshotDevice
  ) const;

// current snapshot set GUID
__declspec(dllexport)
VSS_ID STDMETHODCALLTYPE GetCurrentSnapshotSetId() const;

// Current backup context.  
__declspec(dllexport)
LONG STDMETHODCALLTYPE GetContext() const;

// current app level (either 1,2,3)
__declspec(dllexport)
VSS_APPLICATION_LEVEL STDMETHODCALLTYPE GetCurrentLevel() const;

// determine if path is in set of volumes being snapshotted
__declspec(dllexport)
bool STDMETHODCALLTYPE IsPathAffected
    (
    IN LPCWSTR wszPath
    ) const;

// does the backup include bootable state (formerly system state backup)
__declspec(dllexport)
bool STDMETHODCALLTYPE IsBootableSystemStateBackedUp() const;

// is the backup application smart (i.e., selecting components) or
// dump (i.e., just selecting volumes)
__declspec(dllexport)
bool STDMETHODCALLTYPE AreComponentsSelected() const;

__declspec(dllexport)
VSS_BACKUP_TYPE STDMETHODCALLTYPE GetBackupType() const;

__declspec(dllexport)
VSS_RESTORE_TYPE STDMETHODCALLTYPE GetRestoreType() const;

__declspec(dllexport)
bool STDMETHODCALLTYPE IsPartialFileSupportEnabled() const;

_declspec(dllexport)
    HRESULT STDMETHODCALLTYPE SetWriterFailure(HRESULT hr);

    // Ovverides
    public:
// callback when request for metadata comes in
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnIdentify(IN IVssCreateWriterMetadata *pMetadata);

// callback for prepare backup event
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnPrepareBackup(
    IN IVssWriterComponents *pComponent
    );

// callback for prepare snapsot event
virtual bool STDMETHODCALLTYPE OnPrepareSnapshot() = 0;

// callback for freeze event
virtual bool STDMETHODCALLTYPE OnFreeze() = 0;

// callback for thaw event
virtual bool STDMETHODCALLTYPE OnThaw() = 0;

// callback if current sequence is aborted
virtual bool STDMETHODCALLTYPE OnAbort() = 0;

// callback on backup complete event
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnBackupComplete
    (
    IN IVssWriterComponents *pComponent
    );

// callback indicating that the backup process has either completed or has shut down
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnBackupShutdown
    (
    IN VSS_ID SnapshotSetId
    );

// callback on pre-restore event
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnPreRestore
    (
    IN IVssWriterComponents *pComponent
    );

// callback on post-restore event
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnPostRestore
    (
    IN IVssWriterComponents *pComponent
    );

// callback on post snapshot event
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnPostSnapshot
    (
    IN IVssWriterComponents *pComponent
    );

// callback on back off I/O volume event
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnBackOffIOOnVolume
    (
    IN VSS_PWSZ wszVolumeName,
    IN VSS_ID snapshotId,
    IN VSS_ID providerId
    );

// callback on Continue I/O on volume event
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnContinueIOOnVolume
    (
    IN VSS_PWSZ wszVolumeName,
    IN VSS_ID snapshotId,
    IN VSS_ID providerId
    );

// callback to specify that the volume snaphost service is shutting down.  Used
// by alternative writers to signal when to shutdown.
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnVSSShutdown();

// callback to an alternative writer when the application writer subscribes.  Used to
// signal the alternative writer to shutdown.
__declspec(dllexport)
virtual bool STDMETHODCALLTYPE OnVSSApplicationStartup();

private:

IVssWriterImpl *m_pWrapper;
};

'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵情客 2024-11-25 20:48:30

抽象类是不可实例化的,在大多数情况下,它们用于定义稍后在其他类中实现的接口。当您说要调用抽象类的函数时,您可能想在继承自该抽象类的具体类型上调用这些函数。

例如,假设您有一个提供 area 虚函数的 GeometricFigure 抽象类。您不想获取抽象图形的面积,而是获取具体图形的面积(例如三角形< /代码>)。抽象类为您提供了一种以统一的方式引用不同图形并调用这些对象上的方法的方法,但您不是在抽象图形上调用方法,而是在抽象图形上调用方法一个具体的物体。

An abstract class is not instantiable, and in most cases they are used to define interfaces that are later implemented in other classes. When you say that you want to call functions of the abstract class you are probably wanting to call those functions on a concrete type that inherits from that abstract class.

As an example, consider that you had a GeometricFigure abstract class that provided an area virtual function. You do not want to obtain the area of an abstract figure, but rather the area of a concrete figure (say a Triangle). The abstract class provides you with a way of referring to different figures in an uniform way and calling the methods on those objects, but you are not calling the methods on an abstract figure, you are calling the methods in a concrete object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文