如何调用另一个类的静态方法 [英] How do I call a static method of another class

查看:520
本文介绍了如何调用另一个类的静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,让说 CAppPath 有一个静态方法:

I have a class, lets say CAppPath which has a static method:

public:
    static CString GetAppPath();

,在CAppPath.cpp中定义为:

and in CAppPath.cpp it's defined as:

CString CAppPath::GetAppPath()
{

    return "C:\..\MypAth";
}

现在我有另一个类 CXMLHandler ,我已经包括CAppPath.h。但是如何调用 GetAppPath()方法?我试过:

Now I have another class CXMLHandler, and I have included CAppPath.h in it. But how do I call the GetAppPath() method? I've tried:

#include "CAppPath.h"
void CXMLHandler::MyMethod
{
CNDSClientDlg->GetAppPath();
}

但不起作用。我应该如何访问他的方法。因为它是静态方法,我需要创建一个类的对象或者我需要使该类静态本身?

but it doesn't work. How should I access he method. Since it is static method, do I need to create a object of the class or do I need to make that class static itself?

推荐答案

p>您只需使用范围解析运算符 :: 使用暴露它的类的名称限定方法:

You only need to use the scope resolution operator :: to qualify the method with the name of the class that exposes it:

CString appPath = CAppPath::GetAppPath();

这篇关于如何调用另一个类的静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆