在没有对象的情况下调用 class 函数
Calling class function without objects
以下代码:
#include <iostream>
#include <string>
using namespace std;
class ABC
{
public:
void display ()
{
cout<<"ehhh";
}
};
int main ()
{
using xyz = ABC;
xyz::display();
}
抛出错误:
main.cpp:19:16: 错误:无法在没有对象的情况下调用成员函数‘void ABC::display()’
但是如果我将函数更改为静态函数,它就会工作并给出输出。
static void display ()
{
cout<<"ehhh";
}
我知道静态成员是自动初始化的,这是怎么回事?
调用 class 的非静态函数需要创建一个对象。
您可以通过创建对象或将函数声明为静态来使用代码调用该函数。
// 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();
}
But if I change the function to static it works and gives output.
static
应用于成员的意思是the member is not bound to class instances。对于成员函数,这意味着两件事。首先,this
指针在函数定义中不可用。其次,可以在没有对象的情况下调用函数。
所以,是的,将函数更改为 static
确实允许在没有对象的情况下调用它。
相反,非静态成员函数必须通过class的对象调用。否则,无法定义this
指针。 (this
指针在每个非静态成员函数中都可用,即使未使用该指针。)
如果非virtual
成员函数不使用this
指针,我会倾向于使函数static
。这有助于传达一个事实,即该函数不依赖于特定对象,并且可能使该函数更易于使用(正如您已经发现的那样)。请记住,有时使用 this
时未明确写入“this
”。例如,如果有一个名为 a
的数据成员,那么在成员函数内部,像 a = 0
这样的一行是 this->a = 0
的缩写。查看成员函数中是否使用 this
指针的一种快速方法是使函数 static
并查看编译器是否报错。如果它不抱怨,我倾向于离开函数static
。
I understand static members are initialized automatically, what's happening here ?
你的理解有误,因为没有自动初始化。
静态成员未绑定到 class 个实例。对于数据成员,这意味着某处存储了单个值。如果一个对象更改了该值,则所有其他对象都会看到更改后的值。 (事实上 ,可以在没有可用对象的情况下查看和更改该值。)
这不是关于初始化,而是关于生命周期和实例数。静态数据成员在main
函数开始前是constructed/initialized,在main
函数结束后销毁。它没有在 class 的构造函数中初始化;尝试这样做很可能是错误的。 (也许这就是“自动初始化”误解的起源?)静态数据成员只有一个实例,不管有多少 class 对象存在——即使 [=44= 没有对象] 曾经被创造过。
非静态对象的创建是强制性的functions.The 非静态函数是object/instance 基本函数并且对于每个不同的实例(对象)有不同的行为。单独的内存分配给每个不同的 object.while 在静态静态函数的情况下是 class 函数并且总是只在内存中初始化一次。
以下代码:
#include <iostream>
#include <string>
using namespace std;
class ABC
{
public:
void display ()
{
cout<<"ehhh";
}
};
int main ()
{
using xyz = ABC;
xyz::display();
}
抛出错误: main.cpp:19:16: 错误:无法在没有对象的情况下调用成员函数‘void ABC::display()’
但是如果我将函数更改为静态函数,它就会工作并给出输出。
static void display ()
{
cout<<"ehhh";
}
我知道静态成员是自动初始化的,这是怎么回事?
调用 class 的非静态函数需要创建一个对象。 您可以通过创建对象或将函数声明为静态来使用代码调用该函数。
// 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();
}
But if I change the function to static it works and gives output.
static
应用于成员的意思是the member is not bound to class instances。对于成员函数,这意味着两件事。首先,this
指针在函数定义中不可用。其次,可以在没有对象的情况下调用函数。
所以,是的,将函数更改为 static
确实允许在没有对象的情况下调用它。
相反,非静态成员函数必须通过class的对象调用。否则,无法定义this
指针。 (this
指针在每个非静态成员函数中都可用,即使未使用该指针。)
如果非virtual
成员函数不使用this
指针,我会倾向于使函数static
。这有助于传达一个事实,即该函数不依赖于特定对象,并且可能使该函数更易于使用(正如您已经发现的那样)。请记住,有时使用 this
时未明确写入“this
”。例如,如果有一个名为 a
的数据成员,那么在成员函数内部,像 a = 0
这样的一行是 this->a = 0
的缩写。查看成员函数中是否使用 this
指针的一种快速方法是使函数 static
并查看编译器是否报错。如果它不抱怨,我倾向于离开函数static
。
I understand static members are initialized automatically, what's happening here ?
你的理解有误,因为没有自动初始化。
静态成员未绑定到 class 个实例。对于数据成员,这意味着某处存储了单个值。如果一个对象更改了该值,则所有其他对象都会看到更改后的值。 (事实上 ,可以在没有可用对象的情况下查看和更改该值。)
这不是关于初始化,而是关于生命周期和实例数。静态数据成员在main
函数开始前是constructed/initialized,在main
函数结束后销毁。它没有在 class 的构造函数中初始化;尝试这样做很可能是错误的。 (也许这就是“自动初始化”误解的起源?)静态数据成员只有一个实例,不管有多少 class 对象存在——即使 [=44= 没有对象] 曾经被创造过。
非静态对象的创建是强制性的functions.The 非静态函数是object/instance 基本函数并且对于每个不同的实例(对象)有不同的行为。单独的内存分配给每个不同的 object.while 在静态静态函数的情况下是 class 函数并且总是只在内存中初始化一次。