operator 问题

时间:2008-05-26 11:10:16   来源:论坛整理  作者:  编辑:chinaitzhe
Matrix operator ( const Matrix &m1, const Matrix &m2)
{
return result;
}

在编译器里面会把它转换为下面形式吗?

void operator ( Matrix &result, const Matrix &m1, const Matrix &m2)
{
//直接计算到result中.
}
网友回复:顶一下...........
网友回复:可能..
网友回复:因为c 不能有效的返回一个类对象 所以..
网友回复:不一定,每种编译器的做法不见得相同。
但有可能。
不过命名返回值优化大部分编译器都会做的,所有可以不必太担心效率问题。
网友回复:你那是右元吧,应当不会
网友回复:学习了~~
网友回复:学习下.....~~~~~
网友回复:
引用 3 楼 studyall123 的回复:
因为c 不能有效的返回一个类对象 所以..

Why?

lz的两种写法对编译器而言,没有多大区别!
网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/







#include "iostream"

using namespace std;



struct Matrix {

    union {

        struct {

            float   _11, _12, _13, _14;

            float   _21, _22, _23, _24;

            float   _31, _32, _33, _34;

            float   _41, _42, _43, _44;

        };

        float m[4][4];

    };

};



Matrix operator  (const Matrix& a, const Matrix& b) {

    

    Matrix mat;



    // test

    mat._11 = 1.5f;

    //for (int x = 0; x < 4;   x)

    //    for (int y = 0; y < 4;   y)

    //        mat.m[x][y] = a.m[x][y]   b.m[x][y];

    return mat;

}







int main() {



    Matrix a = {

        1, 1, 1, 1,

        1, 1, 1, 1,

        1, 1, 1, 1,

        1, 1, 1, 1

    };

    Matrix b = {

        1, 1, 1, 1,

        1, 1, 1, 1,

        1, 1, 1, 1,

        1, 1, 1, 1

    };

    Matrix c;

    c = a   b;

    return 0;



}






网友回复:对应上面的asm (vs2005 debug)

C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/







..

 Matrix c;

    c = a   b;

004136F7  lea         eax,[b] 

004136FD  push        eax  

004136FE  lea         ecx,[a] 

00413701  push        ecx  

00413702  lea         edx,[ebp-1DCh] 

00413708  push        edx  

00413709  call        operator  (4110A0h) 

0041370E  add         esp,0Ch 

00413711  mov         ecx,10h 

00413716  mov         esi,eax 

00413718  lea         edi,[ebp-224h] 

0041371E  rep movs    dword ptr es:[edi],dword ptr [esi] 

00413720  mov         ecx,10h 

00413725  lea         esi,[ebp-224h] 

0041372B  lea         edi,[c] 

00413731  rep movs    dword ptr es:[edi],dword ptr [esi] 

    return 0;

00413733  xor         eax,eax 



}

...











Matrix operator  (const Matrix& a, const Matrix& b) {

004113A0  push        ebp  

004113A1  mov         ebp,esp 

004113A3  sub         esp,108h 

004113A9  push        ebx  

004113AA  push        esi  

004113AB  push        edi  

004113AC  lea         edi,[ebp-108h] 

004113B2  mov         ecx,42h 

004113B7  mov         eax,0CCCCCCCCh 

004113BC  rep stos    dword ptr es:[edi] 

    

    Matrix mat;



    // test

    mat._11 = 1.5f;

004113BE  fld         dword ptr [__real@3fc00000 (41574Ch)] 

004113C4  fstp        dword ptr [mat] 

    //for (int x = 0; x < 4;   x)

    //    for (int y = 0; y < 4;   y)

    //        mat.m[x][y] = a.m[x][y]   b.m[x][y];

    return mat;

004113C7  mov         ecx,10h 

004113CC  lea         esi,[mat] 

004113CF  mov         edi,dword ptr [ebp 8] 

004113D2  rep movs    dword ptr es:[edi],dword ptr [esi]                // here

004113D4  mov         eax,dword ptr [ebp 8] 

}

004113D7  push        edx  

004113D8  mov         ecx,ebp 

004113DA  push        eax  

004113DB  lea         edx,[ (4113F0h)] 

004113E1  call        @ILT 130(@_RTC_CheckStackVars@8) (411087h) 

004113E6  pop         eax  

004113E7  pop         edx  

004113E8  pop         edi  

004113E9  pop         esi  

004113EA  pop         ebx  

004113EB  mov         esp,ebp 

004113ED  pop         ebp  

004113EE  ret              




网友回复: 是个二元操作符啊
网友回复:不知,帮顶
网友回复:没碰到过这种情况.
网友回复:
引用 8 楼 qmm161 的回复:
引用 3 楼 studyall123 的回复:
因为c 不能有效的返回一个类对象 所以..

Why?

lz的两种写法对编译器而言,没有多大区别!
似乎有区别?前者能够返回一个值,后者无返回值。
网友回复:学习高级语言时还是不要从底层实现的角度去理解他,这样就偏离了高级语言的目的了。

你所谓的这种转换或者实现,是编译器需要隐藏的细节,各个编译器都可以有自己的实现方法。

引用楼主 luozhi9 的帖子:
Matrix operator ( const Matrix &m1, const Matrix &m2)
{
return result;
}

在编译器里面会把它转换为下面形式吗?

void operator ( Matrix &result, const Matrix &m1, const Matrix &m2)
{
//直接计算到result中.
}

网友回复:对OO相关的东西多思考一点,不要浪费时间在这上面。即使你能得到问题的答案,往往也不能提高你的生产力
关键字:operator,问题,

相关文章

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面