Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

c++ - How do I call a static method of another class

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

public:
    static CString GetAppPath();

and in CAppPath.cpp it's defined as:

CString CAppPath::GetAppPath()
{

    return "C:..MypAth";
}

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 this method? Since it is a static method, do I need to create a object of the class or should I make the class itself static?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...