You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++中const修饰的运算符函数仍可修改成员变量问题

Why Your const Member Function Is Allowing Modifications

Great question—this is a super common point of confusion when working with const member functions and pointer members in C++. Let's break down exactly what's happening here:

1. What const on a Member Function Actually Enforces

When you mark a member function as const, it only guarantees that you cannot modify the member variables of the class itself—not the data those members might point to.

In your StringC class, ps is a char* (a pointer to char). Inside a const member function, this pointer is treated as char* const (a constant pointer to non-constant char). That means:

  • You can't reassign ps to point to a different memory address (e.g., ps = new char[10]; would be illegal)
  • But you can still modify the characters that ps points to (which is exactly what strcat(ps, other.ps) does). This doesn't violate the const constraint because you're not changing the pointer itself—just the data it references.

2. Why other.ps Is Accessible

The other parameter is a const StringC&, so when you access other.ps, it's also treated as char* const. Since strcat's second parameter accepts a const char* (a pointer to constant char), char* const can be implicitly converted to const char*—so this is perfectly legal.

Note: If other.ps pointed to a string literal (e.g., "hello"), modifying that data would cause undefined behavior, but that's a separate issue from the const member function rules.

3. Your operator+ Is Semantically Wrong (And Dangerous)

Beyond the const confusion, your implementation of operator+ violates the expected behavior of the + operator in C++. The + operator should return a new, concatenated string without modifying either operand. Right now, you're modifying the current object's internal string (via strcat), which will surprise anyone using your class (e.g., a + b shouldn't change the value of a).

A Corrected Implementation

Here's how you should write operator+ to follow proper semantics and respect const:

StringC StringC::operator+(const StringC& other) const {
    // Calculate total length including null terminator
    size_t total_len = strlen(ps) + strlen(other.ps) + 1;
    // Allocate memory for the new string
    char* new_str = new char[total_len];
    
    // Copy the current string and append the other
    strcpy(new_str, ps);
    strcat(new_str, other.ps);
    
    // Create a new StringC object with the concatenated string
    StringC result(new_str);
    
    // Clean up if your constructor makes a copy of the input string
    // (If your constructor takes ownership of the pointer, skip this delete)
    delete[] new_str;
    
    return result;
}

Bonus: Fix Memory Management Issues

Your StringC class is missing critical components to avoid memory leaks and shallow copy bugs:

  • A destructor to delete ps when the object is destroyed
  • A copy constructor to properly copy the string (instead of just copying the pointer)
  • Ensure your assignment operator handles deep copies correctly (you have one, but double-check it)

内容的提问来源于stack exchange,提问作者stuart194

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:47:42