我有一个名为Bar的主类调用类Foo,我认为我正确地放入了一个深层构造函数
深拷贝构造函数的目的是将一个对象的内容复制到另一个对象,并且更改复制的对象不应该更改原始内容,对吗?
我的代码那样做,但我不明白为什么当我设置原始对象变量时,复制对象不包含该set变量,它只包含默认的构造函数变量.
public class Bar
{
public static void main(String[] args)
{
Foo object = new Foo();
object.setName1("qwertyuiop");
//the below line of code should copy object to object2?
Foo object2 = new Foo(object);
System.out.println(object.getName1());
//shouldn't the below line of code should output qwertyuiop since object2 is a copy of object? Why is it outputting the default constructor value Hello World?
System.out.println(object2.getName1());
//changes object2's name1 var to test if it changed object's var. it didn't, so my deep copy constructor …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复制构造函数的复制构造函数,或者我可以使用任何类型的递归深度复制机制?
(和cpp失去联系太久了,想复习一下明天的面试)。
正在修订深层复制与浅层复制。写了代码:
#include <iostream>
class MyClass {
public:
unsigned int* uivar = nullptr;
MyClass() : uivar(new unsigned int) {
*(this->uivar) = 3;
}
~MyClass() { delete uivar; }
MyClass(const MyClass& mCopy) {
*(uivar) = *(mCopy.uivar);
}
};
void function(MyClass m) {
*(m.uivar) = 4;
}
int main() {
MyClass myClass;
MyClass myClassCopy = myClass;
std::cout << *(myClass.uivar) << "\n";
std::cout << *(myClassCopy.uivar) << "\n";
function(myClass);
std::cout << *(myClass.uivar) << "\n";
std::cout << "hhhh" << "\n";
*(myClassCopy.uivar) = 5;
std::cout << …Run Code Online (Sandbox Code Playgroud) 我正在编写一个搜索算法来解决java中的15个难题.当我克隆谜题状态以产生新的可能动作(女儿)时,它们仍然相互改变而不是分开.
这是我的克隆方法:
public FifteenPuzzleState clone() throws CloneNotSupportedException
{
FifteenPuzzleState copy = (FifteenPuzzleState)super.clone();
for(int i=0; i<copy.currentConfig.length; i++){
copy.currentConfig[i] = Arrays.copyOf(currentConfig[i], currentConfig[i].length);
}
return copy;
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,在克隆状态然后改变它之后,它也会影响先前的状态和所有其他克隆.我的第一个想法是我没有正确创建2d数组的深层副本,但我发现上面的代码没有任何问题.
有什么建议?谢谢
我正在尝试编写一个程序来执行 的深拷贝List<List<Integer>>,我正在这样做:
public static List<List<Integer>> clone(final List<List<Integer>> src)
{
List<List<Integer>> dest = new ArrayList<List<Integer>>();
for( List<Integer> sublist : src) {
List<Integer> temp = new ArrayList<Integer>();
for(Integer val: sublist) {
temp.add(val);
}
dest.add(temp);
}
return dest ;
}
Run Code Online (Sandbox Code Playgroud)
这是一个好方法吗?是否有可能摆脱内循环?事实上,每个内部子列表都可以增长到很大的长度。
在C++中,有重载的复制构造函数和深复制的赋值重载。由于默认可用的是浅拷贝。
在C中,具有指针成员的结构,如果传递给函数或分配给已经创建的新结构对象是深复制还是浅复制?
例如
struct data {
int a;
int *b;
};
void test_func(struct data tes)
{
tes.a =3;
int *c = new int[1];
c[0]=2;
tes.b =c;
std::cout<<*tes.b;
}
int main() {
struct data test;
int *c = new int;
*c=4;
test.a = 1;
test.b =c;
std::cout<<*test.b;
test_func(test);
std::cout<<*test.b;
}
Run Code Online (Sandbox Code Playgroud) deep-copy ×6
java ×4
c++ ×2
clone ×2
shallow-copy ×2
arraylist ×1
c ×1
constructor ×1
copy ×1
list ×1