上课'方法地址

发布于 2025-02-04 04:24:05 字数 1578 浏览 1 评论 0原文

我要您帮助我理解这个概念。也许我不明白,我不知道..所以我有此示例代码:

#include <iostream>



class X{
    int a;
    public:
        void do_lengthy_work();

};

void X::do_lengthy_work(){
    std::cout << a << std::endl;
}

int main(){
    X my_x;    
    printf("%p\n", &X::do_lengthy_work); // -> does compile
    printf("%p\n", &my_x.do_lengthy_work); // -> doesn't compile,
    // error: ISO C++ forbids taking the address of a bound member function to 
    // form a pointer to member function. Say &X::do_lengthy_work
}

我在一本书中看到了这个代码样本。我认为除非有一个要获得该函数地址的对象,否则我们无法获得类'方法的地址。但是事实证明,我们只能获得类的方法地址,而不是对象的方法地址。我以为每当我们声明一个类的对象时,都会有自己的方法,并带有单独的地址。另外,如果我们执行这样的操作:

#include <iostream>



class X{
    int a;
    public:
        void do_lengthy_work();
        int b;

};

void X::do_lengthy_work(){
    std::cout << a << std::endl;
}

int main(){
    X my_x;
    printf("%p\n", &X::do_lengthy_work);
    printf("%p\n",&X::b);
    printf("%p",&my_x.b);
}

示例输出是:

0x562446a9c17a
0x4
0x7ffe7491a4cc

类方法和对象的变量地址更改。但是B的变量地址不会改变。它始终是0x4,远离0x00的4个字节,因为它是一个int变量。但是,为什么它的地址如此接近0x00,但是函数的地址距离较远?另外 - 重复以前的代码示例中的问题 - 为什么我们无法获得绑定的成员函数的地址,但是我们可以使用类方法?

例如,我们可以做到这一点:

&X::method

但是不是这样:

&object.method

我开始思考 - 如果我对的话,你能纠正我吗? - 在声明类时,一类的变量一次初始化一次(因此,当打印x :: b的地址时,我们会看到0x4的地址对象(因此,我们会看到0x7ffe7491a4cc在打印My_x.b的地址时,但是仅一次初始化方法,每个对象都使用相同的方法,而相同的方法始终在同一地址上?

I'm asking you to help me understand this concept. Maybe I don't understand something, I don't know.. So I have this sample code:

#include <iostream>



class X{
    int a;
    public:
        void do_lengthy_work();

};

void X::do_lengthy_work(){
    std::cout << a << std::endl;
}

int main(){
    X my_x;    
    printf("%p\n", &X::do_lengthy_work); // -> does compile
    printf("%p\n", &my_x.do_lengthy_work); // -> doesn't compile,
    // error: ISO C++ forbids taking the address of a bound member function to 
    // form a pointer to member function. Say &X::do_lengthy_work
}

I saw this sample of code in one book. I thought that we can't get an address of a class' method unless there's an object specified from which we want to get that function's address. But it turns out we can only get a class' method address, but not object's method address. I thought everytime we declare an object of a class, it gets it's own method, with separate address. Also if we do something like this:

#include <iostream>



class X{
    int a;
    public:
        void do_lengthy_work();
        int b;

};

void X::do_lengthy_work(){
    std::cout << a << std::endl;
}

int main(){
    X my_x;
    printf("%p\n", &X::do_lengthy_work);
    printf("%p\n",&X::b);
    printf("%p",&my_x.b);
}

Example output is:

0x562446a9c17a
0x4
0x7ffe7491a4cc

Both class method's and object's variable address change. But the b's variable address does not change. It's always 0x4, 4 bytes further away from 0x00 because it's an int variable. But why it's address is so close to 0x00, but function's address is so further away? Also - repeating my question from previous code sample - why can't we get an address of a bound member function, but we can of a class' method?

So for example we can do this:

&X::method

but not this:

&object.method

I started thinking - can you correct me if I'm right? - variables from a class are initialized once when class is declared (thus we see an address of 0x4 when printing out an address of X::b), and then (uniquely) every other time we specify new object (thus we see 0x7ffe7491a4cc when printing out an address of my_x.b), but methods are initialized only once and every object uses the same method which is always on the same address?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弃爱 2025-02-11 04:24:05

您基本上可以将x视为实现这样的事情:

struct X{
    int a;
    int b;
};
void X__do_lengthy_work(X* this);

方法与正常函数几乎相同,仅在添加了指针的隐藏参数到类实例的情况下。

类的每个实例都使用相同的方法。

将指针指向特定实例的方法可能会产生误导,因为这可能意味着您可以在不提供实例的情况下调用该方法,这就是我猜为什么该语言不允许您这样做。

You can basically think of X as being implemented something like this:

struct X{
    int a;
    int b;
};
void X__do_lengthy_work(X* this);

Methods are pretty much the same as a normal function just with the addition of a hidden parameter of a pointer to the instance of the class.

Each instance of a class uses the same methods.

It could be misleading to take a pointer to a method for a particular instance as that might imply that you could call that method without having to provide the instance which is I guess why the language doesn't allow you to do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文