Why I am able to change member of an object which is passed as constant reference?

RisingGeek
class Student {
private:
    int age;
public:
    char *name;
    Student(int age,char*name) {
        this->age=age;
        this->name=new char[strlen(name)+1];
        strcpy(this->name,name);
    }
    Student(Student const &s1) {
        this->age=s1.age;
        this->name=new char[strlen(s1.name)+1];
        strcpy(this->name,s1.name);
        s1.name[0]='x';
    }
    void display() {
        cout<<age<<" "<<name<<endl;
    }
};
int main() {
    char name[]="abcd";
    Student s1(10,name);
    s1.display();
    Student s2(s1);
    s2.name[0]='x';
    s1.display();
    s2.display();
}

I have passed s1 as a constant reference but I am able to change s1.name[0] and the program compiles successfully. Why I am able to change s1.name[0]?enter image description here

M.M

You did not modify s1. You modified some unrelated memory block (allocated by new) , to which s1 holds a pointer. In C++ there is no special relationship between a raw pointer and any memory block it might happen to be pointing to.

If you use std::string instead of raw pointers and manual memory management then this problem will not arise.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I'm able to access the object (passed through to this method from props) but I am unable to access the object's data

Why am I getting "warning: returning reference to local temporary object"?

Why am I allowed to modify properties which are readonly with object initializers?

Why i am able to change angular constant?

Why am I able to assign a function reference to an anonymous function pointer variable?

Why am I not able to change UIButton text-color?

Why am I not able to access outer class data member through inner class reference?

Why am I able to reference an outer queries columns from within a subquery?

Why a nested object is not passed by reference?

Why member variable which is a pointer to another class is allowed to be changed when the object reference is constant?

Why am I getting an object reference error on my List<> object?

Why am I able to assign the result of require to an object?

Why I am able to assign constant shared_ptr to non constant shared_ptr in C++?

Are DateTimes passed by reference in C#? If not, why is my object updating as I change a variable?

Why am I not able to change the legend title for this sf object plotting?

In JS, why am I able to use functions like toFixed() which resides in the prototype of the Number wrapper object on a primitive type?

Why I am able to modify using constant range based loop?

Why am I getting "Only variables should be passed by reference" error?

Why am I not able to change the value of the boolean variable in this method

Why i am not able to change the dummy variable here in my code?

Why am I not able to change the shell with the chsh command?

Why am I getting the Exception Object reference not set to an instance of an object

I am able to modify a constant string

Why am I receiving a null object reference?

Why am I able to use System.Collections.Generic namespace without having a reference to any assembly?

Why I am not able to add an object to an array? [javascript]

Why am I able to change the array

Why am I not able to clone an object?

Why am i able to set the value to types in a class outside of any member functions? (C++)