How do I call a static method of another class
107,377
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();
Author by
Simsons
Love to write programs . Still learning and trying to explain the code to my self and others.
Updated on July 23, 2022Comments
-
Simsons almost 2 years
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 includedCAppPath.h
in it. But how do I call theGetAppPath()
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?
-
Simsons over 13 yearsThanks, In that case Do I need the method as static?
-
Frédéric Hamidi over 13 years@Subhen, yes, you do. If the method is not static, you'll have to create a new instance of the
CAppPath
class in order to call theGetAppPath()
method on that instance.