主题:当一个函数返回一个类的情况
这个是个测试:并不能通过编译.
主要是我的函数返回一个类时,不能拷贝给另外的函数.
类中定义了普通构造和拷贝构造函数.
不知道怎么回事.
//file:main.cpp
#include <iostream>
#include "test.hpp"
#include <conio.h>
using namespace std;
Test fn()
{
Test n(10);
return n;
}
main()
{
Test aa = fn(); //只有这里通不过.
getch();
}
//file:test.hpp
class Test
{
public:
Test(int);
Test(Test &);
~Test();
protected:
int *a;
int size;
};
//file:test.cpp
#include "test.hpp"
#include <stdlib.h>
Test::Test(int n)
{
a = new int[n] ;
}
Test::Test(Test& s)
{
size = s.size;
a = new int[size];
}
Test::~Test()
{
delete []a;
}
主要是我的函数返回一个类时,不能拷贝给另外的函数.
类中定义了普通构造和拷贝构造函数.
不知道怎么回事.
//file:main.cpp
#include <iostream>
#include "test.hpp"
#include <conio.h>
using namespace std;
Test fn()
{
Test n(10);
return n;
}
main()
{
Test aa = fn(); //只有这里通不过.
getch();
}
//file:test.hpp
class Test
{
public:
Test(int);
Test(Test &);
~Test();
protected:
int *a;
int size;
};
//file:test.cpp
#include "test.hpp"
#include <stdlib.h>
Test::Test(int n)
{
a = new int[n] ;
}
Test::Test(Test& s)
{
size = s.size;
a = new int[size];
}
Test::~Test()
{
delete []a;
}