Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler Does Not Flag Incorrect Operator Usage in Copy Constructor #7254

Closed
BlAcKyCoDe50 opened this issue Sep 15, 2024 · 1 comment
Closed

Comments

@BlAcKyCoDe50
Copy link

Bug Report: Incorrect Operator Usage in Copy Constructor Not Flagged

Description

I have encountered a situation where the compiler does not report an incorrect operator usage in the copy constructor. Specifically, using the subtraction operator (-) where assignment (=) should be used does not result in an error or warning.

Code Example

#include <iostream>
using namespace std;

class Employee {
private:
    int salary;

public:
    string name;
    string dept;
    string Country;
    int contact_info;

    // Friend function declaration
    friend void fun(Employee e);

    // Setter
    void set(int salary) {
        this->salary = salary;
    }

    // Default constructor
    Employee() {
        cout << "Hello from default constructor" << endl;
    }

    // Parametrized constructor
    Employee(string name, string city, string dept, int salary, int contact_info) {
        cout << "Hey from parametrized constructor" << endl;
        this->Country = city;
        this->name = name;
        this->dept = dept;
        this->salary = salary;
        this->contact_info = contact_info;
    }

    // Custom copy constructor with a logical error
    Employee(Employee &orgobj) {
        cout << "Hey from copy constructor" << endl;
        this->contact_info = orgobj.contact_info;
        this->salary - orgobj.salary;  // Incorrect operator used here
    }

    // Print info
    void printinfo()  {
        cout << "Salary: " << salary << endl;
    }
};

// Friend function definition
void fun(Employee e) {
    cout << "Friend function" << endl;
    cout << e.salary << " " << endl;  // Expectation: should print the correct salary
}

int main() {
    Employee e1("name", "INDIA", "Testing", 70000, 1234);
    // e1.printinfo();

    Employee e2(e1); // Invokes copy constructor
    e2.printinfo();  // Prints info using the faulty copy constructor
    fun(e2);         // Prints salary using the friend function

    return 0;
}



Expected Behavior

The compiler should issue a warning or error when the subtraction operator is used where assignment is expected.

Actual Behavior

The code compiles and runs without errors, but the output is incorrect due to the logical error in the copy constructor.

Environment

  • Compiler: [GCC/Clang/MSVC/etc.]
  • Version: [Version number]
  • OS: [Operating System and version]

Impact

This issue can lead to subtle bugs where logical errors are not caught by the compiler, resulting in unexpected behavior and incorrect program output.

@jwakely
Copy link
Member

jwakely commented Sep 15, 2024

w.cc: In copy constructor 'Employee::Employee(Employee&)':
w.cc:41:22: warning: statement has no effect [-Wunused-value]
   41 |         this->salary - orgobj.salary;  // Incorrect operator used here
      |         ~~~~~~~~~~~~~^~~~~~~~~~~~~~~

Compilers do warn about this, if you enable warnings.

In any case, this is not an editorial issue in the draft standard, so does not belong here, see https://github.com/cplusplus/draft/wiki

@jwakely jwakely closed this as not planned Won't fix, can't repro, duplicate, stale Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants