简体   繁体   English

C++中的成员函数是如何实现的?

[英]How member functions are implemented in C++?

I read somewhere that member functions are just like normal function in C++ but with an additional implicit this argument.我在某处读到,成员函数就像 C++ 中的普通 function 一样,但还有一个隐含的this参数。

So I thought this program would be unable to distinguish between two func .所以我认为这个程序将无法区分两个func But the program ran successfully.但程序运行成功。 So was the statement stated above is wrong?那么上面的陈述是错误的吗?

#include <iostream>

class MyCls {
    public:
    void func(int i) {
        std::cout << "Member" << i << std::endl;
    }
};

void func(MyCls m, int i) {
    std::cout << "Outside" << i << std::endl;
}

int main() {
    MyCls m;
    // Thought both would have same signature void func(MyCls, int). But program compiled.
    m.func(4);
    func(m, 5);
    return 0;

Looking at this site , just to pick a tutorial among others if you google the exact same question, you'll find this看看这个网站,如果你用谷歌搜索完全相同的问题,只是为了挑选一个教程,你会发现这个

Member functions are the functions, which have their declaration inside the class definition and works on the data members of the class.成员函数是在 class 定义中声明并作用于 class 的数据成员的函数。 The definition of member functions can be inside or outside the definition of class.成员函数的定义可以在 class 的定义之内或之外。

If the member function is defined inside the class definition it can be defined directly, but if its defined outside the class, then we have to use the scope resolution:: operator along with class name alng with function name. If the member function is defined inside the class definition it can be defined directly, but if its defined outside the class, then we have to use the scope resolution:: operator along with class name alng with function name.

As you can see, a member function is a function which belongs to an instance of a class, having access to object's members and to pointer-to-self-object this如您所见,成员 function 是 function,它属于 class 的实例,可以访问此对象的成员和指向自身对象指针

Of course, in your example you're accessing当然,在您的示例中,您正在访问

m.func(4)

which is member function defined inside object instance of class MyCls whilst writing这是在 class MyCls 的 object 实例中定义的成员MyCls

func(m, 5);

you're passing an object instance of class MyCls to the function in the global scope (by value which is a thing you should study about as well if it's the way you want to act as) without using it, just to verify the coherence of your thoughts. you're passing an object instance of class MyCls to the function in the global scope (by value which is a thing you should study about as well if it's the way you want to act as) without using it, just to verify the coherence of你的意见。

I suggest you to start with a good C++ tutorial to learn about the language's OOP paradigm.我建议你从一个好的 C++ 教程开始学习该语言的 OOP 范式。

functions have different signatures, it can be seen inside list symbols of object file generated:函数有不同的签名,可以在生成的 object 文件的列表符号中看到:

$ g++ -c my.cpp -o my.o
$ nm -s my.o

                 U __cxa_atexit
                 U __dso_handle
                 U _GLOBAL_OFFSET_TABLE_
00000000000000c7 t _GLOBAL__sub_I__Z4func5MyClsi
0000000000000046 T main
000000000000007e t _Z41__static_initialization_and_destruction_0ii
0000000000000000 T _Z4func5MyClsi
0000000000000000 W _ZN5MyCls4funcEi
                 U _ZNSolsEi
                 U _ZNSolsEPFRSoS_E
                 U _ZNSt8ios_base4InitC1Ev
                 U _ZNSt8ios_base4InitD1Ev
                 U _ZSt4cout
                 U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
0000000000000000 r _ZStL19piecewise_construct
0000000000000000 b _ZStL8__ioinit
                 U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc

so answering to your question, it is your second assumption which is wrong: sure, compilers always can distinguish normal functions from class members, by mechanism similar to namespaces (i guess).所以回答你的问题,这是你的第二个假设是错误的:当然,编译器总是可以通过类似于命名空间的机制(我猜)将正常函数与 class 成员区分开来。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM