简体   繁体   English

如果我尝试在 c++ 中打印类的非静态成员函数的地址,为什么会创建 object?

[英]Why an object is created if I try to print a class's non-static member function's address in c++?

I found one interesting behavior while I was just checking non-static member variable's offsets我在检查非静态成员变量的偏移量时发现了一种有趣的行为

It looks like if I try to print non-static member function's address in pointer看起来如果我尝试在指针中打印非静态成员函数的地址
It just creates a new object for the class which has the member function它只是为具有成员 function 的 class 创建一个新的 object

Is this a normal behavior or undefined behavior that I can just ignore?这是我可以忽略的正常行为还是未定义的行为?

for instance if I try below code例如,如果我尝试下面的代码

#include <iostream>

typedef void(*FP)();

class AAA {
public:
    int var1;
    int var2;

    void foo() {
        std::cout << &var1 << std::endl;
        std::cout << &var2 << std::endl;
    }
};

int main() {
    FP fp = reinterpret_cast<FP>(&AAA::foo);
    std::cout << fp << std::endl;
    fp();
}

Its result is like below它的结果如下

$ ./a.out
0x56001ea8d940
0x7fe36c93c760
0x7fe36c93c764

So I removed cout statement then it just prints member's offset like I expected所以我删除了cout语句然后它只是像我预期的那样打印成员的偏移量

#include <iostream>

typedef void(*FP)();

class AAA {
public:
    int var1;
    int var2;

    void foo() {
        std::cout << &var1 << std::endl;
        std::cout << &var2 << std::endl;
    }
};

int main() {
    FP fp = reinterpret_cast<FP>(&AAA::foo);
    fp();
}

and its result is like below结果如下

$ g++ main.cpp (with some warnings anyway)
$ ./a.out
0x1
0x5

The weird behaviour you're seeing is undefined behaviour caused by the following line:您看到的奇怪行为是由以下行引起的未定义行为:

FP fp = reinterpret_cast<FP>(&AAA::foo);

Even without calling fp this is already undefined behaviour!即使不调用fp这已经是未定义的行为! Only the listed conversions are valid for reinterpret_cast ( [expr.reinterpret.cast] ), and the one you're using (member function pointer to function pointer) is not one of them. 只有列出的转换reinterpret_cast[expr.reinterpret.cast] )有效,而您正在使用的转换(成员 function 指针指向 function 指针)不是其中之一。

It's a common misunderstanding that reinterpret_cast is an “anything goes” cast that allows circumventing type safety in any way imaginable.一个常见的误解是reinterpret_cast是一种允许以任何可以想象的方式规避类型安全的“任何事情”转换。 According to the standard, that's not the case;根据标准,情况并非如此; only a handful of specific conversions are valid.只有少数特定的转换是有效的。 All others are not (but do not require diagnosis, which leads to them being used anyway).所有其他人都不是(但不需要诊断,这导致它们无论如何都被使用)。

暂无
暂无

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

相关问题 为什么有一个额外的&将非静态成员函数的地址传递给C ++中的线程? - Why is there an extra & to pass the address of a non-static member function to a thread in C++? c++ 为什么非静态成员 function 是prvalue? - c++ why non-static member function is a prvalue? 让静态成员函数在 C++ 中调用非静态成员 std::function 的正确方法是什么? - What's the correct way to let the static member function call a non-static member std::function in c++? 在 C++17 中的对象生命周期之外调用非静态成员 function - Calling non-static member function outside of object's lifetime in C++17 指向模板类的非静态成员函数的C ++函数指针(类成员) - C++ function pointer (class member) to non-static member function of a template class C ++函数指针(类成员)到非静态成员函数 - C++ function pointer (class member) to non-static member function 静态线程函数访问C ++中的非静态类成员 - static thread function access non-static class member in C++ 为什么C ++中的函数重载解析将静态调用的非静态成员函数考虑在内? - Why does function overload resolution in C++ consider non-static member functions for static calls? 将函数作为参数传递给C ++中的pthread时,为什么会出现“非静态成员函数”错误? - Why do I get a “non-static member function” error when passing a function as a parameter to a pthread in C++? 没有构造函数的类中的C ++非静态const成员 - C++ non-static const member in class without a constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM