C++ Static Member Method Call on Class Instance

C++ Static member method call on class instance

The standard states that it is not necessary to call the method through an instance, that does not mean that you cannot do it. There is even an example where it is used:

C++03, 9.4 static members

A static member s of class X may be referred to using the
qualified-id expression X::s; it is
not necessary to use the class member access syntax (5.2.5) to refer
to a static member. A static member
may
be referred to using the class member access syntax, in which
case the object-expression is
evaluated.

class process {
public:
static void reschedule();
};

process& g();

void f()
{
process::reschedule(); // OK: no object necessary
g().reschedule(); // g() is called
}

C++ calling a static method

There's the obvious difference in that the first version has to construct and destruct a Foo.

Then there's the obvious similarity in that both versions do the same thing when the function call is executed (construct a string, print, etc).

The less obvious difference, is in the evaluation of the two expressions. You see, even though foo is not required for the call, it's still evaluated as part of the expression:

[class.static]/1

A static member s of class X may be referred to using the qualified-id
expression X​::​s; it is not necessary to use the class member access
syntax to refer to a static member. A static member may be referred to
using the class member access syntax, in which case the object
expression is evaluated.

In your case that doesn't mean anything. But in certain contexts, it can prevent your program from compiling at all. For instance, if foo was instead a reference parameter in a constexpr function.

C++: Static member function returning an object of self static for a class with private constructor

As @tkausi says, the static member function can create an object, as it is a member (thus having access to private methods.)

What this code does is having an instance of the class, only one object (_self) and returning it for use.

Why nobody gets the return value? Because the call is there only to create the instance. If you don't call the function no _self object will be created.

class CAbc
{
private:
CAbc() { cout << "creating" << endl; }
public:
static CAbc& getInstance()
{
static CAbc _self;
return _self;
}
};

int main() {
cout << "Begin" << endl;

//CAbc::getInstance();
}

With the call to getInstance commented out you won't see the "creating" output.

If you uncomment the call, you'll see:

Begin
creating

Once created, the function will always return the same object.

Accessing private member variables of a class from a static method

static myClassPtr create(unsigned int val) {

create() is a static method of myClass, it is a member of this class. As such it it entitled to access all private members and methods of its class. This right extends not only to its own class instance, but any instance of this class.

As per my understanding private members should not be accessible.

... except by members of their class.

Let's create a completely pointless copy constructor for your class, the same copy constructor you would get by default:

myClass(const myClass &o) : m_val{o.m_val} {}

This copy constructor has no issues, whatsoever, of accessing m_val of the passed-in object. Exactly the same thing happens here. m_val is a private member of its class. It doesn't mean that only an instance of the same exact object can access its private members, it means that any instance of the class, or a static class method, can access the private class members.

C++ static member functions and variables

Static methods can't use non-static variables from its class.

That's because a static method can be called like Environment::display() without a class instance, which makes any non-static variable used inside of it, irregular, that is, they don't have a parent object.

You should consider why you are trying to use a static member for this purpose. Basically, one example of how a static method can be used is as such:

class Environment
{
private:
static int maxRobots;
public:
static void setMaxRobots(int max)
{
maxRobots = max;
}
void printMaxRobots();
};

void Environment::printMaxRobots()
{
std::cout << maxRobots;
}

And you would have to initialize on the global scope the variables, like:

int Environment::maxRobots = 0;

Then, inside main for example, you could use:

Environment::setMaxRobots(5);

Environment *env = new Environment;
env->printMaxRobots();
delete env;


Related Topics



Leave a reply



Submit