简体   繁体   中英

(C++) Const-Reference modified?


I'm trying to write a string class which can perform most of std::string's actions. Actually, I'm stuck with my "insert" function.

template <typename T>
class string_base {
public:
... 
/// INSERT FUNCTION 
// len = current string length of *this
// cap = current capacity of this->raw_data
// raw_data = array which holds all characters
// DEF_ALLOC = 8192
// T = type of element (char, wchar_t, char16_t, char32_t), in my case: char
// str.data() returns the char array of the "str" parameter 
// str.length() returns "len" variable of "str"

string_base<T> &insert(const string_base<T> &str, unsigned pos) {
    if (pos > len || !str.length()) return *this;
    unsigned o = len;
    if (cap <= (len += str.length())) {
        cap += (str.length() + DEF_ALLOC);
        raw_data = (T *)realloc(raw_data, (cap * sizeof(T)));
    }
    if (pos) {
        for (unsigned i = o; i >= pos; i--)
            raw_data[i + str.length()] = raw_data[i];
    } else {
        for (unsigned i = o; i > 0; i--)
            raw_data[i + str.length()] = raw_data[i];
        raw_data[str.length()] = raw_data[0];
    }
    for (unsigned i = pos; i < (pos + str.length()); i++)
        raw_data[i] = str.data()[i - pos];
    raw_data[len] = 0x00;
    return *this;
}
private:
T *raw_data;
unsigned len, cap;
};

typedef string_base<char> string;
typedef string_base<wchar_t> wstring;
typedef string_base<char16_t> string16;
typedef string_base<char32_t> string32;

As we can see the function takes a parameter as a const-reference (Which can't be changed by the function as far as I know) I call the function like that:

string_base<char> a("Roses are red"); // assign "Roses are red" to a's char array
string_base<char> b("not "); 
//a.insert(b, 10); -> this works correctly  
a.insert(a, 10); // when I pass "a", it does shit

I insert "a" into "a" at position 10
"a" (it's char array) should now have a value of "Roses are Roses are redred".
Instead of that, it has a value of "Roses are Roses are Roses ".
I might need to add that the function itself works fine, I just have a problem with this const reference...

I think the "insert" function modifies the passed parameter as well, but it shouldn't.
Is there any way how I can prevent or fix that?

Thanks in advance! I hope my question is clear enough.

You cannot modify an object through a reference-to-const. That doesn't mean you can't modify that object through other references though.

As a simple example, the following is perfectly valid and will print "100":

int main() {
    int i = 42;
    int& ri = i;
    const int& cri = i;

    ri = 100;
    std::cout << cri;
}

The changes made through ri are reflected through cri since both refer to the same object.

Your call to a.insert(a, 10) is the same situation. str and this both refer to the same object. Any changes you make to the current object will also be reflected through str . That means when you shift characters around in the reallocated buffer you're modifying the data that will be copied.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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