免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1617 | 回复: 2
打印 上一主题 下一主题

Linux下编写C++遇到了些问题··(具体问题,请进贴,谢谢) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-09-30 09:36 |只看该作者 |倒序浏览
各位编程高手们:

最近我在学习C++,看到了复制构造函数这一块,遇到些问题,想请教一下大家,vc和vim中编写C++的一些区别,一下是源代码:我的困惑也在下面

#include <iostream>
#include <cstring>
using namespace std;

class mystring
{
        char         *s;

        public:
                mystring (char *str);    //普通构造函数声明
                mystring (const mystring &obj);  //自定义复制构造函数声明
                ~mystring ()
                {
                        if (s)
                                delete [] s;
                        cout << "Freeing s\n";
                }
                void show ()
                {
                        cout << s << "\n";
                }
};

mystring::mystring (char *str)
{
        s = new char[strlen(str) + 1];
        cout << "Normally constructor called\n";
        strcpy (s, str);
               
}

mystring::mystring (const mystring &obj)
{
        s = new char[strlen(obj.s) + 1];
        cout << "Copy constructor called \n";
        strcpy (s, obj.s);
}

mystring input ()
{
        char instr[80];

        cout << "Please input a string:";
        cin >> instr;

        mystring ob (instr);   //在此会调用普通构造函数
       
        return ob;             //在此会调用自定义复制构造函数
}

int main (void)
{
        mystring obj = input ();  //调用自定义复制构造函数

        obj.show ();

        return 0;
}

本应该的运行结果是:
Please input a string : hello
Normal constructor called.
Copy constructor called.
Freeing s.
hello
Freeing s.
在vc中的运行结果确实也是这样,但是在linux下,运行的结果却是没有调用复制构造函数:
Please input a string : hello
Normal constructor called.
hello
Freeing s.

我有些困惑,出现这样的结果是因为编译环境的问题么?还是其他原因?
如果想让程序运行出预想的结果应该怎么做?

我的描述可能不太准确,因为刚刚开始学习的。。
谢谢大家先。。

论坛徽章:
0
2 [报告]
发表于 2010-09-30 16:20 |只看该作者
看看vc和linux下的汇编结果 .

论坛徽章:
0
3 [报告]
发表于 2010-10-14 00:09 |只看该作者
可能是标准不一样吧,VC默认的入栈出栈使用__stdcall,但是LInux下好像用的是标准C的入栈出栈方式,C BUILER是 __fastcall
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP