TigerCode TigerCode - 2 months ago 14
C++ Question

C++ - Creating a Copy function without = assignment for a Dynamic String Array

Trying to write a copy function for a dynamically allocated array.

In my header file I have:

#include <memory>
#include <string>


using std::string;
using std::unique_ptr;
using std::make_unique;

class StringSet{
public:
//create an empty set
StringSet() = default;
StringSet(int capacity);

//copy a set
StringSet(const StringSet&);

StringSet& operator[](const int);

//Insert a string to the set
bool insert(string);

//Remove a string from the set
bool remove(string);

//Test whether a string is in the set
int find(string) const;

//Get the size of the set
int size() const;

//get string at position i
string get(int i) const;

//Return the set union of the set and another StringSet
StringSet setunion(const StringSet&) const;

//Return the intersection of the set and another StringSet
StringSet intersection(const StringSet&) const;

//Return the set diffference of the set and another StringSet
StringSet difference(const StringSet&) const;

//prevent default copy assignment
StringSet& operator=(const StringSet&) = delete;

int NOT_FOUND = -1;
static constexpr int def_capacity {4};

private:
int arrSize {def_capacity};
int currentSize {0};
unique_ptr<string[]> arr {make_unique<string[]>(def_capacity)};

};


In my implementation file I have:

#include "StringSet.h"
#include <iostream>
#include <utility>



StringSet::StringSet(int capacity)
: arrSize{capacity},
arr{make_unique<string[]>(capacity)}
{
}

StringSet::StringSet(const StringSet& a)
{
auto a2 = StringSet(currentSize);

for (auto i=0; i < currentSize ; i++ )
{
a2[i] = a[i];
}
}


Compiler error:

error: constructors may not be cv-qualified
error: no match for 'operator=' (operand types are 'StringSet' and 'std::string {aka std::basic_string<char>}')
error: passing 'const StringSet' as 'this' argument discards qualifiers [-fpermissive]
error: use of deleted function 'StringSet& StringSet::operator=(const StringSet&)'


My assignment has overloaded the assignment operator= and thus I'm not able to use that here. Is there another way of implementing a copy function without using the assignment operator - is there anything in std::string that allows us to copy contents easier in this manner?

If there's anything else I need to add here for details please let me know.

Thank you.

Answer

The problem with this code:

StringSet::StringSet(const StringSet& a)
{
    auto a2 = StringSet(currentSize);

    for (auto i=0; i < currentSize ; i++ )
    {
        a2[i] = a[i];
    }
}

is that, even if it compiled, you're never actually initializing the members of this... you're initializing some temporary a2 that goes out of scope at the end of the constructor. You actually want:

StringSet::StringSet(const StringSet& a)
    : StringSet(a.arrSize)
{
    currentSize = a.currentSize;

    for (auto i=0; i < currentSize; i++ )
    {
        arr[i] = a.arr[i];
    }
}

Also, your operator[] returns a StringSet& where it should probably return a std::string&.

Also, you should avoid bringing names into the global namespace like you're doing. Keep it local. Writing std:: is not a burden.

Comments