Naval Kishore
Naval Kishore

Reputation: 145

Calling class function without objects

The following code:

#include <iostream>
#include <string>

using namespace std;
class ABC
{
    public:
    void display ()
    {   
        cout<<"ehhh";
    }
};


int main ()
{
  using xyz = ABC;
  xyz::display();
  
}

Throws an error: main.cpp:19:16: error: cannot call member function ‘void ABC::display()’ without object

But if I change the function to static it works and gives output.

static void display ()
    {   
        cout<<"ehhh";
    }

I understand static members are initialized automatically, what's happening here ?

Upvotes: 0

Views: 1146

Answers (3)

Rana Muhammad Usama
Rana Muhammad Usama

Reputation: 302

Object creation is compulsory for the non-static functions.The non-static functions are object/instance base functions and have different behavior for every different instances(objects). separate Memory is allocated to every different object.while in case of static static-functions are the class Functions and always get initialized in memory only once.

Upvotes: 0

JaMiT
JaMiT

Reputation: 16843

But if I change the function to static it works and gives output.

The meaning of static when applied to a member is that the member is not bound to class instances. For member functions, that means two things. First, the this pointer is not available inside the function definition. Second, the function can be called without an object. So, yes, changing the function to be static does allow it to be called without an object.

In contrast, a non-static member function must be called through an object of the class. Otherwise, there is no way to define the this pointer. (The this pointer is available in every non-static member function, even if the pointer is not used.)

If a non-virtual member function does not use the this pointer, I would be inclined to make the function static. This helps convey the fact that the function has no dependency on a particular object, and it might make the function easier to use (as you have already discovered). Keep in mind that sometimes this is used without explicitly writing "this". For example, if there is a data member named a, then inside a member function a line like a = 0 is short for this->a = 0. A quick way to see if the this pointer is used in a member function is to make the function static and see if the compiler complains. If it doesn't complain, I tend to leave the function static.

I understand static members are initialized automatically, what's happening here ?

Your understanding is off, as there is no automatic initialization.

A static member is not bound to class instances. For data members, that means that there is a single value stored somewhere. If one object changes that value, then all other objects see the changed value. (In fact, the value can be seen and changed without having an object available.)

This is not about initialization but about lifetime and number of instances. A static data member is constructed/initialized before the main function starts and is destroyed after the main function ends. It is not initialized within the constructor of the class; an attempt to do so is likely an error. (Perhaps this is the origin of the "initialized automatically" misconception?) There is only one instance of a static data member, regardless of how many objects of the class exist – even if no objects of the class are ever created.

Upvotes: 0

risingStark
risingStark

Reputation: 1155

Calling a non-static function of a class requires creation of an object. You can call the function using the code by creating an object or by declaring the function as static.

// Calling display function by creating an object.
#include <iostream>
#include <string>

using namespace std;
class ABC
{
    public:
    void display ()
    {
        cout<<"ehhh";
    }
};


int main ()
{
  using xyz = ABC;
  xyz obj; // You can also write --> ABC obj;
  obj.display();
}
// Calling display function by declaring the member function as static
#include <iostream>
#include <string>

using namespace std;
class ABC
{
    public:
    static void display ()
    {
        cout<<"ehhh";
    }
};


int main ()
{
  using xyz = ABC;
  xyz::display(); // You can also use ABC::display();
}

Upvotes: 2

Related Questions