Static Class Data Elements and Constructors

How to access a static member in a class with all static methods?

I want to have a group of related functions, but there are also some important data members initialized before any of these functions are called. I thought that a class consisting only of static members would be a way for you. I do not like the compiler in VS2008 when I try to access "a".

Of course, I am missing something small, but still very embarrassed.: P (Even without invalid access "a", the constructor is not called when the testMethod () method is called from the main one.

class IPAddressResolver
{
private:

public:
    static int a;
    IPAddressResolver();
    static void TestMethod();
};


IPAddressResolver::IPAddressResolver()
{
    IPAddressResolver::a = 0;
    cout << "Creating IPAddressResolver" << endl;
}

void IPAddressResolver::TestMethod()
{
    cout << "testMethod" << endl;
}
+3
source share
3 answers

,

class IPAddressResolver
{
private:
    static int a;
    IPAddressResolver();
public:
    static void TestMethod();
};

int IPAddressResolver::a = 0;

void IPAddressResolver::TestMethod()
{
    cout << "testMethod" << endl;
}

, . , . private, , (. ).

:

+11

- , .

int IPAddressResolver::a = 0;

IPAddressResolver.cpp.

+2

I want to have a group of related functions, but also have some important data elements initialized before any of these functions will be called

It seems to me that you want Singleton, not a class with only static members.

+2
source

Source: https://habr.com/ru/post/1748922/


All Articles