0

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.

1
  • 2
    The best practice would be to use std::vector and/or std::unqiue_ptr, and leaving your class with the default destructor and copy/move constructors (rule of zero)
    – Red Alert
    Jul 28, 2015 at 21:02

1 Answer 1

4

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.

2
  • 1
    Thank you for such a detailed answer. I guess I have a lot more learning to do. I'm sort of following a course outline that is teaching me how all this stuff works and isn't "real world" acceptable based on what you've written. (which makes sense on why my assignment notes are all written so bizarrely). So my question probably reflects my lack of understanding still. At least I learned what it is I'm trying to do, instead of what's considered best practices. Thank you. Jul 28, 2015 at 21:31
  • 1
    @user2470057 if you do want to manage your own resources, instead of using std::string, best practice is to have one object per resource. So instead of your constructor having three new expressions, your class should have 3 objects (possibly of the same class as each other), each of which has a single new expression in its constructor.
    – M.M
    Jul 28, 2015 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.