admin管理员组

文章数量:1124552

By experiment, it seems CStringT::MakeReverse and CStringT::MakeUpper all change the string in-place.

What is the keyword on CStringT Class documentation, which I should pay attention to, saying these operations are "in-place"?

Returning a value implies the methods create new copies in my newbie's opinion.

By experiment, it seems CStringT::MakeReverse and CStringT::MakeUpper all change the string in-place.

What is the keyword on CStringT Class documentation, which I should pay attention to, saying these operations are "in-place"?

Returning a value implies the methods create new copies in my newbie's opinion.

Share Improve this question edited 2 days ago wohlstad 27.2k16 gold badges54 silver badges85 bronze badges asked 2 days ago Jason ChoJason Cho 331 silver badge6 bronze badges 11
  • 4 If the function is not const assume it can modify the string. Something like MakeSomething is a typical clue that it will be modify because you are making it into a specific condition. – NathanOliver Commented 2 days ago
  • 4 Those functions return a reference (CStringT&), so I guess you can assume that means it returns itself. – 001 Commented 2 days ago
  • 2 In addition to the points mentioned above, I would say the description of the mehotds (e.g. "Reverses the order of the characters in the CStringT object.") hints that the current object will be modified. – wohlstad Commented 2 days ago
  • 1 Many libraries are using naming convention where method name uses verb in past participle to indicate this is none mutating functions (return value will provide outcome) and present tense for function which mutate object. – Marek R Commented 2 days ago
  • 2 Note that CStringT is a Microsoft thing, not standard C++. The question is about how to read Microsoft's documentation, often a difficult proposition. – Pete Becker Commented 2 days ago
 |  Show 6 more comments

1 Answer 1

Reset to default 4

Collecting the good points mentioned in the comments above:

There are several points in the CStringT documentation that give hints that the methods are mutating the current object in place:

  • The main hint is that the methods return a reference - CStringT&.
    If they were creating a new object it would have been returned by value.
    There is no other object which it make sense to return a refernce to here, execpt for the current one.

  • Additional points that supports this:

    1. The methods are not marked as const. Although it is not a must, methods that do not modify the object should be marked as const, and the lacking of it hints that these methods do modify it.

    2. The name of the methods starting with Make usually hint that they modify the current object to "make" it something else.

    3. The documentation text, e.g. "Reverses the order of the characters in the CStringT object." hints that the current object will be modified.

本文标签: cHow to tell if a class method changes the object inplace (in Microsoft39s documentation)Stack Overflow