简体   繁体   English

如何在不需要多个新运算符的情况下编写此代码?

[英]how to write this code without the need of more than one new operator?

I have a generic code with template <class T> that I want it to work without the need of T() so for the copy constructor I have this:我有一个带有template <class T>的通用代码,我希望它在不需要T()情况下工作,所以对于复制构造函数,我有这个:

template <class T>
SortedList<T>::SortedList(const SortedList<T>& list):
  data(new T*[list.max_size])
 ,size(list.size)
 ,max_size(list.max_size)
 {
     for (int i = 0; i < size; i++)
     {
         T* new_element=new T(*(list.data[i]));//my problem
        data[i]=new_element;
     }
 }

and it works fine , and I don't have any valgrind errors or any kind of error But I have learned that it is not good coding to have more than one new in your code, so I want to write a code that works without the new so I tried this :并且它工作正常,我没有任何 valgrind 错误或任何类型的错误但是我了解到在代码中包含多个new代码不是很好的编码,所以我想编写一个无需new所以我试过这个:

T* new_element=& T(*(list.data[i]));

here there is an error : taking address of temporary这里有一个错误:取临时地址

does anyone know what should I do now?有谁知道我现在该怎么办?

If you do this:如果你这样做:

template <class T>
SortedList<T>::SortedList(const SortedList<T>& list):
  data(new T*[list.max_size])
 ,size(list.size)
 ,max_size(list.max_size)
 {
     for (int i = 0; i < size; i++)
     {
         T* new_element=new T(*(list.data[i]));
        data[i]=new_element;
     }
 }

You are copying all the elements of the other SortedList<T> .您正在复制其他SortedList<T>所有元素。 I am not sure where you heard this:我不确定你是从哪里听到的:

I have learned that it is not good coding to have more than one new in your code.我了解到在您的代码中包含多个新代码并不是好的编码。

It is a good practice to stay away from dynamic allocation and use pre-coded containers.远离动态分配并使用预编码容器一种很好的做法。 From the sound of SortedList , I'd say you want a std::set instead.SortedList的声音来看,我会说你想要一个std::set

If it is acceptable for your container to use the other container's elements, you can do this:如果您的容器可以使用其他容器的元素,您可以这样做:

template <class T>
SortedList<T>::SortedList(const SortedList<T>& list):
  data(new T*[list.max_size])
 ,size(list.size)
 ,max_size(list.max_size)
 {
     for (int i = 0; i < size; i++)
     {
         T* new_element= list.data[i];
        data[i]=new_element;
     }
 }

This could pose some problems , because if you modify the first SortedList<T> 's elements, it would modify the second's, so you probably don't want to do this.这可能会带来一些问题,因为如果您修改第一个SortedList<T>的元素,它会修改第二个的元素,因此您可能不想这样做。

The reason you cannot do this:你不能这样做的原因:

T* new_element=& T(*(list.data[i]));

Is because *list.data[i] is a temporary variable, and it is destructed as soon as you exit the current iteration of the for loop.是因为*list.data[i]是一个临时变量,一旦退出for循环的当前迭代它就会被破坏。 If you take it's address, as soon as you try to modify it, you will get an error, as the element does no longer exist.如果你获取它的地址,一旦你尝试修改它,你就会得到一个错误,因为该元素不再存在。 (It has been destructed.) I would recommend that you keep your code the same for now, or even better, use a different container. (它已被破坏。)我建议您现在保持代码相同,或者更好的是,使用不同的容器。

Another answer to this question proposes using this:这个问题的另一个答案建议使用这个:

std::memcpy(data, list.data, list.size * sizeof(T))

The problem with this, is, as @AndyG stated:问题在于,正如@AndyG 所说:

Gotta be really careful with something like this.遇到这样的事情一定要小心。 It will only work for trivially relocatable types, not generic T.它只适用于普通的可重定位类型,而不适用于泛型 T。

The other answer also proposed doing std::copy .另一个答案还建议做std::copy This may work for your purpose, as it is higher level, and does all the work with new behind the scenes.这可能适用于您的目的,因为它是更高级别的,并且在幕后使用new完成所有工作。 To use it, you would do this:要使用它,你可以这样做:

std::copy(list.data[0], list.data[list.size-1], data[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM