复制构造函数需要?? [英] Necessary for copy constructor??

查看:52
本文介绍了复制构造函数需要??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序是否会导致任何问题,因为''test''类缺少

显式复制构造函数?


#include< ; vector>

#include< iostream>


班级考试

{

char * cPtr;


public:


test()

{

cPtr =新字符[10];

}


~test()

{

删除cPtr ;

}

};

int main()

{

std ::矢量< test * vecTest;


test * testPtr = new test();


vecTest.push_back(testPtr);


删除testPtr;


vecTest.clear();

}

Will the following program cause any problem, because of the missing
explicit copy constructor for the ''test'' class?

#include <vector>
#include <iostream>

class test
{
char* cPtr ;

public:

test()
{
cPtr = new char[10] ;
}

~test()
{
delete cPtr ;
}
} ;
int main()
{
std::vector< test*vecTest ;

test* testPtr = new test() ;

vecTest.push_back( testPtr ) ;

delete testPtr ;

vecTest.clear() ;
}

推荐答案

qa********@rediffmail.com 写道:

以下程序是否会导致任何问题,因为''test''类缺少

显式复制构造函数?


#include< vector>

#include< iostream>


班级测试

{

char * cPtr;


public:


test()

{

cPtr = new char [10];

}


~test()

{

删除cPtr;

}

};


int main( )

{

std :: vector< test * vecTest;


test * testPtr = new test();


vecTest.push_back(testPtr);


删除testPtr;


vecTest.clear();

}
Will the following program cause any problem, because of the missing
explicit copy constructor for the ''test'' class?

#include <vector>
#include <iostream>

class test
{
char* cPtr ;

public:

test()
{
cPtr = new char[10] ;
}

~test()
{
delete cPtr ;
}
} ;
int main()
{
std::vector< test*vecTest ;

test* testPtr = new test() ;

vecTest.push_back( testPtr ) ;

delete testPtr ;

vecTest.clear() ;
}



不。由于指针有自己的复制构造函数,你可以在

STL容器中自由使用它们。


问候,

Stuart

Nope. As pointers have their own copy constructor, you can use them in
STL containers freely.

Regards,
Stuart




qazmlp1 ... @ rediffmail.com skrev:

qazmlp1...@rediffmail.com skrev:

请问以下程序导致任何问题,因为''test''类缺少

显式复制构造函数?
Will the following program cause any problem, because of the missing
explicit copy constructor for the ''test'' class?



是的。你将有双重删除。根据你的实际需要,你需要使用std :: vector或std :: string而不是低级指针。

实际上,operator new []应该几乎不会使用。首选std :: vector

(或者在极少数情况下是raw operator new)


/ Peter

Yes. You will have double deletes. Depending on your real needs, you
should use std::vector or std::string instead of the lowlevel pointer.
In fact, operator new[] should almost never be used. Prefer std::vector
(or in rare situations raw operator new)

/Peter


>

#include< vector>

#include< iostream>


班级测试

{

char * cPtr;


public:


test()

{

cPtr = new char [10];

}


~test()

{

删除cPtr;

}

};


int main( )

{

std :: vector< test * vecTest;


test * testPtr = new test();


vecTest.push_back(testPtr);


删除testPtr;


vecTest.clear();

}
>
#include <vector>
#include <iostream>

class test
{
char* cPtr ;

public:

test()
{
cPtr = new char[10] ;
}

~test()
{
delete cPtr ;
}
} ;
int main()
{
std::vector< test*vecTest ;

test* testPtr = new test() ;

vecTest.push_back( testPtr ) ;

delete testPtr ;

vecTest.clear() ;
}


peter koch写道:
peter koch wrote:

qazmlp1 ... @ rediffmail.com skrev:
qazmlp1...@rediffmail.com skrev:

以下程序是否会导致任何问题,因为''test''类缺少

显式复制构造函数?
Will the following program cause any problem, because of the missing
explicit copy constructor for the ''test'' class?



是的。你将有双重删除。


Yes. You will have double deletes.



你是对的,一般来说(每个有指针成员的类应该

都有正确定义的显式构造函数)。

但是,你可以评论是否会导致我在main()中给出的代码有问题吗?

You are right, in general (Every class having a pointer member should
have an explicit constructor defined correctly).
But, could you comment whether it will cause problem for the code that
I have given within main()?


根据你的实际需要,你需要使用std :: vector或std :: string而不是低级指针。

实际上,operator new []应该几乎不会使用。首选std :: vector

(或在极少数情况下为raw operator new)
Depending on your real needs, you
should use std::vector or std::string instead of the lowlevel pointer.
In fact, operator new[] should almost never be used. Prefer std::vector
(or in rare situations raw operator new)


这篇关于复制构造函数需要??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆