标题:创建空间
取消只看楼主
shaoyuan
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2008-9-7
结帖率:100%
 问题点数:0 回复次数:0 
创建空间
#include<stdlib.h>
#include<iostream.h>
class shaoyuan{
public:
    shaoyuan(int y){ x=y;
        cout<<"begin\n";
    }   void sy(){cout<<"adfasdfas"<<"   "<<x<<endl;}
    ~shaoyuan(){
        cout<<"end\n";
    }private: int x;
};
void main(){
    int *p=(int *)malloc(sizeof(int));
         shaoyuan *p1=(shaoyuan*)malloc(sizeof(shaoyuan(10)));
    p1->sy(); free (p1);
}
请问为什么在执行shaoyuan *p1=(shaoyuan*)malloc(sizeof(shaoyuan(10)));时不会执行构造函数而且在释放空间的时候也不会执行析构函数,还有就是p1->sy(); 输出的x的值不是10,谢谢!!!
搜索更多相关主题的帖子: 空间 
2008-09-11 16:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-232717-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.095492 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved