请帮帮忙,简单的C++问题?
很简单,但为什么运行不正确???#include <iostream>
#include <cstdlib>
using namespace std;
class myclass
{
int *p;
public:
myclass(int i);//普通构造函数
myclass(const myclass &obj); //复制构造函数
~myclass();
int getval(){return *p;}
}
myclass::myclass(const myclass &obj)
{
p = new int;
*p = *obj.p;
cout<<"Copy constructor called.\n";
}
myclass::myclass(int i)
{
cout<<"Allocating";
p = new int;
*p = i;
}
myclass::~myclass()
{
cout<<"Freeing p\n";
delete p;
}
void display(myclass ob)
{
cout<<ob.getval()<<'\n';
}
int main()
{
myclass a(10);
display(a);
return 0;
}