C/C++ Copy Assignment Operator for CarCounter Class

Click For Summary
The discussion focuses on implementing a copy assignment operator for the CarCounter class in C++. The operator should assign the carCount from one object to another and return the current object. The initial code provided contains several errors, including a missing closing brace and incorrect variable references. The correct implementation checks for self-assignment and directly assigns the carCount from the source object to the destination object. There is a consensus that the assignment operator should typically return a const reference to the object, although opinions vary on this point. The main goal is to ensure that the assignment operator effectively makes one CarCounter object an exact copy of another.
kstewa17
Messages
1
Reaction score
0
Write a copy assignment operator for CarCounter that assigns objToCopy.carCount to the new objects's carCount, then returns *this. Sample output for the given program:

Cars counted: 12

Code:
#include <iostream>
using namespace std;

class CarCounter {
   public:
      CarCounter();
      CarCounter& operator=(const CarCounter& objToCopy);
      void SetCarCount(const int setVal) {
         carCount = setVal;
      }
      int GetCarCount() const {
         return carCount;
      }
   private:
      int carCount;
};

CarCounter::CarCounter() {
   carCount = 0;
   return;
}

// FIXME write copy assignment operator

/* Your solution goes here  */

[B]CarCounter& CarCounter::operator=(const CarCounter& objToCopy) {
  
   
   if (this != &objToCopy) {           
      delete setVal;                  
      setVal = new int;               
      *setVal = *(objToCopy.dataObj); 
   }
   
   return *this;[/B]

int main() {
   CarCounter frontParkingLot;
   CarCounter backParkingLot;

   frontParkingLot.SetCarCount(12);
   backParkingLot = frontParkingLot;

   cout << "Cars counted: " << backParkingLot.GetCarCount();

   return 0;
}
 
Technology news on Phys.org
Re: C/C++ (Copy Assignment)...What I have so far is is red, and it's confusing me a little.

Code:
CarCounter& CarCounter::operator=(const CarCounter& objToCopy) {
   if (this != &objToCopy) {
      delete setVal;
      setVal = new int;
      *setVal = *(objToCopy.dataObj);
   }
   return *this;
}

First, this post belongs in the computer science section. Your code is missing
a closing } so it can't compile. Furthermore, there is no field of CarCounter
that is named setval. Again compiler error. Apparently, you tried to mimic a
correct assignment operator for a different class. Your main setup is correct
but the details are all wrong.

An assignment operator = is meant to assign an exact copy of ObjToCopy to
the object on the left side of the =. So, for example, if you have two objects
car1 and car2 of class CarCounter, car1=car2 should have the effect of making
car1 an exact copy of car2.

Here's a correct assignment operator:

Code:
const CarCounter& CarCounter::operator=(const CarCounter& objToCopy) {
   if (this != &objToCopy) {
      carCount = objToCopy.getCarCount();
   }
   return *this;
  }
}

"Most" people want the assignment operator to return a const reference to
an object of the class, but there is some disagreement about this.
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
53
Views
5K
Replies
10
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 23 ·
Replies
23
Views
2K
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K