0

More C++ learning questions. I've been using vectors primarily with raw pointers with a degree of success, however, I've been trying to play with using value objects instead. The first issue I'm running into is compile error in general. I get errors when compiling the code below:

class FileReference {

    public:
        FileReference(const char* path) : path(string(path)) {};
        const std::string path;

};

int main(...) {

    std::vector<FileReference>  files;

    // error C2582: 'operator =' function is unavailable in 'FileReference'
    files.push_back(FileReference("d:\\blah\\blah\\blah"));

}

Q1: I'm assuming it's because of somehow specifying a const path, and/or not defining an assignment operator - why wouldn't a default operator work? Does defining const on my object here even I'm assuming it's because I defined a const path, Does const even win me anything here?

Q2: Secondly, in a vector of these value objects, are my objects memory-safe? (meaning, will they get automatically deleted for me). I read here that vectors by default get allocated to the heap -- so does that mean I need to "delete" anything.

Q3: Thirdly, to prevent copying of the entire vector, I have to create a parameter that passes the vector as a reference like:

// static
FileReference::Query(const FileReference& reference, std::vector<FileReference>& files) {
    // push stuff into the passed in vector
}

What's the standard for returning large objects that I don't want to die when the function dies. Would I benefit from using a shared_ptr here or something like that?

Community
  • 1
  • 1
ansiart
  • 2,563
  • 2
  • 23
  • 41

2 Answers2

5
  1. If any member variables are const, then a default assignment operator can't be created; the compiler doesn't know what you would want to happen. You would have to write your own operator overload, and figure out what behaviour you want. (For this reason, const member variables are often less useful than one might first think.)

  2. So long as you're not taking ownership of raw memory or other resources, then there's nothing to clean up. A std::vector always correctly deletes its contained elements when its lifetime ends, so long as they in turn always correctly clean up their own resources. And in your case, your only member variable is a std:string, which also looks after itself. So you're completely safe.

  3. You could use a shared pointer, but unless you do profiling and identify a bottleneck here, I wouldn't worry about it. In particular, you should read about copy elision, which the compiler can do in many circumstances.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
3

Elements in vector must be assignable from section 23.2.4 Class template vector of the C++ standard:

...the stored object shall meet the requirements of Assignable.

Having a const member makes the class unassignable.

As the elements are being stored by value, they will be destructed when the vector is destroyed or when they are removed from the vector. If the elements were raw pointers, then they would have to be explicitly deleted.

hmjd
  • 120,187
  • 20
  • 207
  • 252