C ++复制构造函数与重载分配与构造函数最佳实践? [英] C++ copy constructor vs overloaded assignment vs constructor best practices?

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

问题描述

这更多是一个意见/最佳实践问题。



我是C ++的新手,目前正在开发使用动态分配字符串的程序。最后,我得到了构造函数,复制构造函数和重载的赋值运算符之间的区别。我还需要这些对象的析构函数。



(我正在构建一个考试对象,其中包含问题对象,这些问题对象包含T / F答案对象数组,



这是我的问题:在专业领域中,创建这些对象的最佳实践是什么?当我坐在这里思考时,我可以从用户那里收集信息并将这些值存储在临时位置,并使用构造函数实例化问题对象,或者可以使用复制和分配方法来构建每个对象...不知道该怎么办。一种方法比另一种更好吗?我应该构建并测试所有这三个吗?请帮忙。

解决方案

这种情况下的最佳做法是不要自己管理资源。使用标准库( std :: string std :: vector / std ::地图)。像这样的东西:

  #include< string> 
#include< map>

班级考试{
public:
std :: map< std :: string,std :: string>问题_;
};

std :: string 进行资源管理为了你。调用其析构函数时,它将在其后面进行清理。 std :: map std :: vector 也是如此。



这里的魔力在于所有3个类中的成员( std :: string std :: map RAII 。因此,如果您这样使用它:

  void foo(){
考试e;
e.questions _ [ 6 x 7 =?] = 42;
} //在这里,您可以保证所有问题和答案的所有存储空间都已被丢弃。

您不必担心编写构造函数,析构函数或管理堆分配的对象。 / p>

通常,我建议尝试避免编写 new delete 在您的程序中。如果需要在不太适合容器的用例中进行动态分配,请使用 std :: unique_ptr std :: shared_ptr 。尽力遵守零法则


this is more of an opinion/best practices question.

I'm new to C++ and I'm currently working on a program that uses dynamically allocated strings. I finally get the difference between the constructors, the copy constructors and overloaded assignment operator. I also get the need for a destructor for these objects.

(I'm building an exam object that holds question objects that hold an array of T/F answer objects, each point to dynamic storage for the strings).

Here's my question: What is considered best practices in the professional world for creating these object? As I sit here and think about this, I can gather information from the user and store those values in temporary locations and instantiate the question objects with the constructor, or I can build each object using the copy and assignment methods... I'm not sure what to do. Is one method better than the other? Should I build and test all three? Please help.

解决方案

Best practice in this case is to not manage resources yourself. Use the standard library (std::string and std::vector/std::map in this case). Something like:

#include <string>
#include <map>

class Exam {
public:
  std::map<std::string, std::string> questions_;
};

std::string does the resource management for you. When its destructor is called, it'll clean up behind it. The same goes for std::map or std::vector.

The magic here is that the members in all 3 classes (std::string, std::map, Exam) are guaranteed to be properly disposed as they go out of scope, per RAII. So, if you use it like:

void foo() {
  Exam e;
  e.questions_["6 x 7 = ?"] = "42";
} // Here, you're guaranteed that all storage for all your questions and answers is disposed.

you don't have to worry about writing constructors, destructors, or managing heap-allocated objects.

Generally, I'd recommend trying to avoid writing new and delete in your programs at all. If you need dynamic allocation in a use-case that doesn't fit well with containers, use std::unique_ptr or std::shared_ptr. Try to abide by the Law of Zero as much as you can.

这篇关于C ++复制构造函数与重载分配与构造函数最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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