0

I was following a tutorial on https://www.learncpp.com/ but I have some doubts...I have a simple class with many overloaded operators and a few member functions. I do not know where should I put const and which method should return-by reference and which by-value. For the simplicity let us say that it is a box with numbers:

class Box {
 private:
    unsigned int d;  // dimension
    float *arr;  // elements
 public:
    explicit Box(unsigned int arg_d, float* arg_arr);
    Box(const Box& B);
    Box() = delete;
    ~Box();
    float _() const { return d; }
    float norm() const;
    Box inv() const;
    Box expand(unsigned int arg_d);
    Box operator~ () const;
    Box operator- () const;
    Box& operator= (const Box &B);
    float& operator[] (unsigned int i);
    const float& operator[] (unsigned int i) const;
    Box& operator+= (const Box &B);
    Box& operator-= (const Box &B);
    Box& operator*= (const Box &B);
    Box& operator^= (const unsigned int x);
    Box& operator/= (const Box &B);
};
  • Many operators (and a copy constructor too) take const arguments. Do I have to write non-const versions too?
  • Some operator (as well as the method _()) have a keyword const after the argument list. Should I have that for all? Or should I write two versions of everything?
  • Should I mark the return type as const like in the [] operator? Right now this one has two versions, should it be like that? Let's say I would allow for modification of class element with []. Should I even write const version? Let's say I do not allow it - should I even write non-const version?
  • Operators which are linked with assignment return by-reference (I found that in the tutorial). Is that correct? Should other operators, like ~ and - which return new objects also return by reference?

On top of that I have overloaded the operators globally too:

bool operator== (const Box &B1, const Box &B2);
bool operator!= (const Box &B1, const Box &B2);
Box operator+ (const Box &B1, const Box &B2);
Box operator- (const Box &B1, const Box &B2);
Box operator* (const Box &B1, const Box &B2);
Box operator^ (const Box &B, const unsigned int x);
Box operator/ (const Box &B1, const Box &B2);
  • Do I have to write versions which accept non-const arguments too?
  • Should these operators have const keyword at the end, like [] previously? I guess not, since then the new object could not be modified? Or am I confusing the const that appears before return type with the const after the argument list? What is the difference?
  • Should these operators return a new Box by reference, as for their assignment-chained counterparts?
maciek
  • 1,807
  • 2
  • 18
  • 30
  • Your functions only need to accept non-`const` arguments if you intend to modify those arguments during execution of the function. `operator==` isn't _changing_ `B1` or `B2` so you don't need a version that accepts non-const `Box` references as arguments. – Nathan Pierson Nov 08 '20 at 18:23
  • please try to be more focused, like one question per question. If you ask one only, then very likely the others will be already answered by that, and if not you can ask more questions – 463035818_is_not_an_ai Nov 08 '20 at 18:24
  • You may also find [this](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) to be a useful question for discussions of operator overloading and why some return types are references and some are values. – Nathan Pierson Nov 08 '20 at 18:24
  • 2
    One question per stackoverflow.com question, please. It's understandable why are you confused. "Becomes a C++-uberhacker in 21 minutes"-kind of web sites are usually cobbled together, and are all over the place. Any clown can put together a web site that says anything. You will usually find a much more organized explanation and an in-depth discussion, of the most complicated general purpose programming language in use today, including these and many other topics [in one of many quality C++ textbooks](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Nov 08 '20 at 18:25
  • fwiw, I voted to close because needs more focus, but given that all questions are about operator overloading it could be flagged as dupe of this as well: https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading – 463035818_is_not_an_ai Nov 08 '20 at 18:30

0 Answers0