C++ 将值返回到C+中的类对象+;
我有一个头文件:C++ 将值返回到C+中的类对象+;,c++,class,object,C++,Class,Object,我有一个头文件: using namespace std; class IntList{ private: int *Intl; int Capacity; int Count; public: IntList(int capacity){ Capacity = capacity; Count = 0; Intl = new int[capacity]; } ~IntList(){
using namespace std;
class IntList{
private:
int *Intl;
int Capacity;
int Count;
public:
IntList(int capacity){
Capacity = capacity;
Count = 0;
Intl = new int[capacity];
}
~IntList(){
delete Intl;
}
//adds the integers of the specified collection to the end of the List; return false if the new Count will be greater than Capacity
bool AddRange(const IntList &items){
//int *Temp = items.;
if(items.Count > Capacity - Count){
return false;
}else{
for(int i = 0; i <items.Count; i++){
Intl[Count] = items.Intl[i];
Count++;
}
return true;
}
}
};
使用名称空间std;
类内部列表{
私人:
int*Intl;
国际能力;
整数计数;
公众:
整数列表(整数容量){
容量=容量;
计数=0;
Intl=新的int[容量];
}
~IntList(){
删除Intl;
}
//将指定集合的整数添加到列表末尾;如果新计数大于容量,则返回false
布尔添加范围(常量输入列表和项目){
//int*Temp=项目。;
如果(items.Count>容量-计数){
返回false;
}否则{
对于(int i=0;i,因为int*Intl;
是手动管理的对象,所以需要为类实现复制构造函数
函数GetRange
按值返回。本地对象A
被销毁,其成员Intl
在析构函数中被删除,因此您的副本(由默认复制构造函数复制)仅为浅副本,将包含无效成员
编辑:正如Rob正确指出的,您还需要实现赋值运算符(您已经有了析构函数)。因为int*Intl;
是您手动管理的对象,所以您需要为类实现复制构造函数
函数GetRange
按值返回。本地对象A
被销毁,其成员Intl
在析构函数中被删除,因此您的副本(由默认复制构造函数复制)仅为浅副本,将包含无效成员
编辑:正如Rob正确指出的,您还需要实现赋值操作符(您已经有了析构函数).对于通过值返回的对象,调用复制构造函数。
您必须创建一个复制构造函数并定义它,以便获得适当的结果。通过引用返回实际上不需要调用复制构造函数,但不应为临时对象创建。另外,因为您有一个指针类型作为成员va类中的变量。您可以重载=
运算符。应该正确定义它以避免内存泄漏
。执行以下操作Intlist a=GetRange(index,count)
。您还应该为此创建一个副本构造函数。您的代码还有一个bug,它不会重载类Intlist的=
运算符
您可以编写类似以下内容的复制构造函数:-
Intlist::Intlist(const Intlist& cSource)
{
capacity = cSource.capacity;
count= cSource.count;
// Intl is a pointer, so we need to deep copy it if it is non-null
if (cSource.Intl)
{
// allocate memory for our copy
Intl = new int[capacity];
// Copy the Intl into our newly allocated memory in for loop
for(i=0;i<capacity;i++)
{
// copy part
}
}
else
intl = NULL;
}
Intlist::Intlist(常量Intlist&cSource)
{
容量=cSource.capacity;
count=cSource.count;
//Intl是一个指针,所以如果它不是null,我们需要对它进行深度复制
if(cSource.Intl)
{
//为我们的副本分配内存
Intl=新的int[容量];
//将Intl复制到for循环中新分配的内存中
对于(i=0;i对于通过值返回的对象,调用复制构造函数。
您必须创建一个复制构造函数并定义它,以便获得适当的结果。通过引用返回实际上不需要调用复制构造函数,但不应为临时对象创建。另外,因为您有一个指针类型作为成员v类中的变量。您可以重载=
运算符。应该正确定义它,以避免内存泄漏
。执行以下操作Intlist a=GetRange(index,count)
。您还应该为此创建一个副本构造函数。您的代码还有一个bug,它不会重载类Intlist的=
运算符
您可以编写类似以下内容的复制构造函数:-
Intlist::Intlist(const Intlist& cSource)
{
capacity = cSource.capacity;
count= cSource.count;
// Intl is a pointer, so we need to deep copy it if it is non-null
if (cSource.Intl)
{
// allocate memory for our copy
Intl = new int[capacity];
// Copy the Intl into our newly allocated memory in for loop
for(i=0;i<capacity;i++)
{
// copy part
}
}
else
intl = NULL;
}
Intlist::Intlist(常量Intlist&cSource)
{
容量=cSource.capacity;
count=cSource.count;
//Intl是一个指针,所以如果它不是null,我们需要对它进行深度复制
if(cSource.Intl)
{
//为我们的副本分配内存
Intl=新的int[容量];
//将Intl复制到for循环中新分配的内存中
对于(i=0;我还可以看到和。请参见和。我有一个头文件:使用名称空间std;
您不应该在头文件中添加using指令。我有一个头文件:使用名称空间std;
您不应该在头文件中添加using指令。@LuchianGrigore并不是要通过引用返回临时对象。i只是想让他知道,在这种情况下,对复制构造函数的调用是不可能的made@LuchianGrigore已修改。感谢您的建议:)我试图修复它,但它没有返回值?@user1366448您是否为它编写了一个复制构造函数。如果是,您可以发布该代码。同时发布调用GetRange函数的代码//复制构造函数IntList(IntList&range){Intl=new int;*Intl=*range.Intl;cout@LuchianGrigore并不是要通过引用返回临时对象。我只是想让他知道,在这种情况下,对复制构造函数的调用将不会被调用made@LuchianGrigore已修改。感谢您的建议:)我试图修复它,但它没有返回值?@user1366448您是否为它编写了一个复制构造函数。如果是,您可以发布该代码。还可以发布调用GetRange函数的代码。//复制构造函数IntList(IntList&range){Intl=new int;*Intl=*range.Intl;cout