问题:
matrix m1 = new matrix(); // should produce a matrix of 3*3
matrix m2 = new matrix(5,4); //5*4
matrix m3 = new matrix(m2); //5*4
Run Code Online (Sandbox Code Playgroud)
在复制构造函数中应该有什么来制作与m2相同顺序的新矩阵m3?
public class matrix {
int a[ ][ ];
matrix(){
a = new int[3][3];
}
matrix(int x, int y){
a= new int [x][y];
}
matrix (matrix b1){
//how to use value of x and y here....
}
void show(){
System.out.println(a.length);
for(int i=0;i<a.length;i++){
System.out.print(a[i].length);
}
}
}
public class matrixtest {
public static void main(String …Run Code Online (Sandbox Code Playgroud) 我想创建无法复制的类,所以我将复制构造函数放入私有部分:
class NotCopyable
{
public:
NotCopyable(const double& attr1, const double& attr2) : _attr1(attr1), _attr2(attr2) {}
~NotCopyable(void) {}
private:
NotCopyable& operator=(const NotCopyable&);
NotCopyable(const NotCopyable&);
double _attr1;
double _attr2;
};
Run Code Online (Sandbox Code Playgroud)
一切都很好,除非我想分配数组:
NotCopyable arr[] =
{
NotCopyable(1, 0),
NotCopyable(2, 3)
};
Run Code Online (Sandbox Code Playgroud)
编译器说她无法访问复制构造函数,因为它在私有部分.当我把它放在公共部分时:
class NotCopyable
{
public:
NotCopyable(const double& attr1, const double& attr2) : _attr1(attr1), _attr2(attr2) {}
~NotCopyable(void) {}
NotCopyable(const NotCopyable&)
{
std::cout << "COPYING" << std:: endl;
}
private:
NotCopyable& operator=(const NotCopyable&);
double _attr1;
double _attr2;
};
Run Code Online (Sandbox Code Playgroud)
程序编译没有错误,但不调用复制构造函数.所以问题是:我如何禁止复制但仍有可能分配数组?
由编译器为未定义自己的类合成的默认复制构造函数是正确的:它将所有成员从一个对象复制到另一个对象.
我想做类似的事情.我想写一个方法
template <typename T>
T f(const T& obj) {
// for each member var i of obj, I want to call some method g(obj.i)
}
Run Code Online (Sandbox Code Playgroud)
现在我不知道成员变量的名称是什么.如果这是复制构造函数,我可以调用赋值运算符而不是g.
很明显,编译器会这样做(但也许它会在推断出类成员的名称之后执行此操作).是否有可能为任何T级课程做到这一点?
我试图解决我的程序中的一些问题,看起来我的复制构造函数或我的析构函数有问题.我得到了一个内存异常.
任何帮助我都会感谢谢谢
ArrayStorage::ArrayStorage(const ArrayStorage &a):readArray(a.readArray),arraysize(a.arraysize)
{
readArray = new string[arraysize]; //create the array
memcpy (readArray,a.readArray,sizeof(string)*arraysize);//Copy the values of bytes from the location pointed at by the souce and destination.
}
ArrayStorage::~ArrayStorage(void)
{
delete[](readArray);//deconstuctor to delete the array.
}
Run Code Online (Sandbox Code Playgroud)
这将是一个更好的方法来复制除memcpy之外的数组:
for (int i = 0 ; i < arraysize ; i ++)
{
readArray[i] = a.readArray[i];
}
Run Code Online (Sandbox Code Playgroud) 具体来说,如果original为null,则抛出NullPointerException.然而javadocs没有提到它.
我知道,它说这个构造函数不应该是必要的,除非需要一个显式的副本,但我正在为包含字符串的一些较大的对象编写复制构造函数,虽然它可能不是绝对需要制作一个显式的副本,因为其他一切在这种情况下得到一份明确的副本,我愿意付出低效率的低价.
但是这个javadoc不应该有抛出?
我正在编写一个用于克隆对象的复制构造函数.当一个类引用一个对象时,该对象被其他几个类进一步说明.
class Person
{
String name;
Address address;
}
class HomeAdress extends Address
{
}
class OfficeAdress extends Address
{
}
Run Code Online (Sandbox Code Playgroud)
现在在Person的复制构造函数中,要决定要传递哪个Address对象,我必须使用instanceof.
public Person(Person p)
{
name = p.name;
if(p.address instanceof HomeAddress)
{
address = new HomeAddress((HomeAddress) address);
}else if(p.address instanceof OfficeAddress)
{
address = new OfficeAddress((OfficeAddress) address);
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当将新类型的地址添加到模型时,这是基本问题.我将不得不在Person复制构造函数中添加相同的检查.有没有办法避免instanceof检查实例化正确的地址对象.我可以使用refelction来避免代码中的instanceof吗?
我遇到了复制构造函数的继承问题.当我尝试复制一个类时,不会调用父类的复制构造函数.这是一个总结问题的示例程序:
#include <iostream>
//parent class
class cParent
{
public:
//parent data
int iParentData;
//default constructor
cParent(void) : iParentData(0) {}
//copy constructor
cParent(const cParent& SOURCE) : iParentData(SOURCE.iParentData) {}
};
//child class
class cChild : public cParent
{
public:
//child data
int iChildData;
//default constructor
cChild(void) : iChildData(0) {}
//copy constructor
cChild(const cChild& SOURCE) : iChildData(SOURCE.iChildData) {}
};
int main()
{
cChild SourceClass; //create a class
SourceClass.iParentData = 10; //and set some values
SourceClass.iChildData = 10; //
cChild CopyClass(SourceClass); //use the copy …Run Code Online (Sandbox Code Playgroud) Heyho,看看这堂课
public class ComplexFoo {
TreeMap<String, String> field1;
double[] field2;
public ComplexFoo() {
field1 = new TreeMap<String, String>();
field2 = new double[1];
}
public ComplexFoo(ComplexFoo cf){
field1 = cf.field1;
field2 = cf.field2;
}
public static void main(final String[] args) {
ComplexFoo foo = new ComplexFoo();
foo.field2[0] = 5.0;
ComplexFoo bar = new ComplexFoo(foo);
bar.field2[0] = 3.0;
System.out.println(foo.field2[0]);
}
}
Run Code Online (Sandbox Code Playgroud)
这打印3.0.我想这是因为我的复制构造函数只复制了对Array/Map的引用,而不是真正的数据结构.我需要更改什么才能确保深度复制?
如果我编写一个将ComplexFoo作为字段的类,我是否必须编写一个使用ComplexFoos复制构造函数的复制构造函数,或者我可以使用任何类型的递归深度复制机制?
我有以下代码来测试复制ctor并移动std::string类的ctor,结果让我感到惊讶,移动ctor 比复制ctor慢约1.4倍.
据我所知,移动构造不需要分配内存,因为在这种std::string情况下,移动构造对象中可能有一个内部指针直接设置为移动对象的内部指针,它应该比为缓冲区分配内存更快然后在复制构造时复制对象中的内容.
这是代码:
#include <string>
#include <iostream>
void CopyContruct(const std::string &s) {
auto copy = std::string(s);
}
void MoveContruct(std::string &&s) {
auto copy = std::move(s);
//auto copy = std::string(std::move(s));
}
int main(int argc, const char *argv[]) {
for (int i = 0; i < 50000000; ++i) {
CopyContruct("hello world");
//MoveContruct("hello world");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑:
从这两个函数的汇编中,我可以看到,因为MoveConstruct有一个std::remove_reference类模板的实例化,我认为这应该是罪魁祸首,但我不熟悉汇编,任何人都可以详细说明吗?
以下代码在https://godbolt.org/上使用x86-64 gcc7.2进行了反编译:
CopyContruct(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
push rbp
mov …Run Code Online (Sandbox Code Playgroud) 我按照三个规则实施了一个类,我遇到了崩溃.在调试时,我得出结论,复制构造函数反复调用自身而不是调用相等运算符.为什么会这样?它不应该调用相等运算符吗?
#include <iostream>
#include <deque>
#include <cstdlib>
#define LENGTH 128
typedef struct tDataStruct
{
char strA[LENGTH];
char strB[LENGTH];
int nNumberOfSignals;
double* oQueue;
tDataStruct()
{
nNumberOfSignals = 0;
//oQueue = NULL;
memset(strA, 0, LENGTH);
memset(strB, 0, LENGTH);
}
~tDataStruct()
{
if (NULL != oQueue)
{
delete[] oQueue;
oQueue = NULL;
}
}
tDataStruct(const tDataStruct& other) // copy constructor
{
if (this != &other)
{
*this = other;
}
}
tDataStruct& operator=(tDataStruct other) // copy assignment
{
if (this == &other)
{ …Run Code Online (Sandbox Code Playgroud) copy-constructor ×10
c++ ×6
java ×4
inheritance ×2
arrays ×1
c++11 ×1
class ×1
cloning ×1
deep-copy ×1
destructor ×1
move ×1
performance ×1
string ×1