简体   繁体   English

可以在不创建实际对象的情况下通过指针访问类的函数吗?

[英]Can functions of class be accessed by pointers, without creating actual objects?

I was going through following code: 我正在通过以下代码:

#include <iostream>
using namespace std;
class B
{
    public:
        int a;
       void display()
         { 
             a=10;
             cout<<"Content of base class.\n"; }
};
int main()
{
    B *b,b1,b2;
    char *a=new char[1];
    *a='A';
    if(b==NULL)
    {
        cout<<(*(int*)a)<<endl;
    }
    b->display();
    b1.display();
    b2.display();
    b->a=60;
    b1.a=40;
    b2.a=50;
    cout<<b->a<<endl<<b1.a<<endl<<b2.a;
    return 0;
}

It is giving the following output: 它给出以下输出:

65
Content of base class.
Content of base class.
Content of base class.
60
40
50

According to my knowledge for every object created some space is allocated and the data of that object is stored there(Here b1 and b2).But in this code b is just a pointer and still it is able to access function and variables of class B and it is not even pointing any object of class Bb is NULL as indicated by if statement.I am getting confused with this!!! 据我了解,对于创建的每个对象,都会分配一些空间并将该对象的数据存储在此处(此处b1和b2)。但是在此代码中b只是一个指针,仍然可以访问B类的函数和变量而且它甚至没有指向if语句所指示的Bb类的任何对象为NULL。对此我感到困惑!

Now I also want to know where b->a (whose value is 60) is stored?Like 40 and 50 will be there in b1 and b2. 现在我还想知道b-> a(值为60)存储在哪里?就像b1和b2中有40和50一样。

Also, if only pointers are sufficient to call the functions then is it really necessary to create objects everytime? 另外,如果仅指针足以调用函数,那么是否真的有必要每次创建对象?

It's undefined behaviour . 这是未定义的行为 The output you received, and the fact that the program did not crash, is nothing but a coincidence. 您收到的输出以及程序没有崩溃的事实,只是一个巧合。

If you don't need an object, use static or free-standing functions. 如果不需要对象,请使用静态或独立功能。

b->display(); yields undefined behavior. 产生未定义的行为。 That means, whatever your computer does at that moment is good enough. 这意味着,此时您的计算机所做的一切都足够好。

The question of where b->a is stored makes no sense, because accessing b->a yields undefined behavior. b->a的存储位置问题毫无意义,因为访问b->a产生未定义的行为。

In some cases, C++ is not obliged to tell you that you screwed it up. 在某些情况下,C ++没有义务告诉您您将其搞砸了。 Dereferencing a pointer to uninitialized memory is such a case. 取消引用未初始化内存的指针就是这种情况。

Considering your running what you have there: (unified diff: http://hastebin.com/midocuqiki ), you will get: 考虑到您在那里运行的东西:(统一差异: http : //hastebin.com/midocuqiki ),您将获得:

$ ./b
Segmentation fault (core dumped)

actually, when you compile: 实际上,当您编译时:

$ g++ -Wall B.cpp -o b
B.cpp: In function ‘int main()’:
B.cpp:11:5: warning: ‘b’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     if(b==NULL)
     ^

It is undefined behavior . 这是未定义的行为 You got that output, because Visual Studio (and that's what I think you use), is optimizing to use your previous stack and you're going to the same location in memory where you actually have something. 之所以会得到输出,是因为Visual Studio(这就是我所使用的)正在优化以使用先前的堆栈,并且您将到达内存中实际有东西的相同位置。

But is completely wrong! 但这是完全错误的!

and YES , it is "really necessary to create objects every time" ! ,必须“每次都创建对象”

UPDATE : @codecracker says this behavior is on gcc as well! 更新 :@codecracker说此行为也在gcc (weird) (奇怪的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C++ 中,成员函数是否只能由相同 class 的对象访问,即使该 function 的访问说明符是公共的? - In C++, can the member functions be ONLY accessed by the objects of same class, even if the access specifier for that function is public? C ++-创建指向包含类对象指针的数组元素的指针 - C++ - Creating pointers to elements of an array containing pointers to class objects 每个 class 的功能是否在该 class 的对象内具有 function 指针? - Do the functions of each class have function pointers inside the objects of that class? 使用指针创建函数 - Creating functions using pointers 带有指向对象的指针的运算符功能 - Operator functions with pointers to objects 使用指向明确加载的DLL函数的静态指针创建类 - Creating a class with static pointers to explicitly loaded DLL functions 在不创建任何类型对象的情况下声明向量和指针 - Declaring vectors and pointers without creating any type objects 在C ++对象中:我应该使用父类强制转换指针还是应该使用实际的类本身强制转换 - In C++ objects: Should I be casting pointers with the parent class or should I be casting with the actual class itself 在编译时创建具有不同功能的相同类的对象 - Creating Objects of same Class with different Functions at Compiletime 从静态函数创建类对象 - Creating Class Objects from Static Functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM