How to enforce immutability to prevent the modification of values in C++

6 Answers

0 votes
#include <iostream>

// Immutable variables using const
// const variables — prevent modification

int main() {
    const int x = 10;   // x is immutable

    std::cout << "x = " << x << std::endl;

    // x = 20;  // ERROR: assignment of read‑only variable
}


/*
run:

x = 10

*/

 



answered 17 hours ago by avibootz
edited 16 hours ago by avibootz
0 votes
#include <iostream>

// Immutable objects using const member functions

class User {
private:
    int id;          // mutable internally
    std::string name;

public:
    User(int id, std::string name) : id(id), name(name) {}

    // Mark getters as const → cannot modify object
    int getId() const { return id; }
    std::string getName() const { return name; }

    // void setName(std::string n) { name = n; }  // not allowed if we want immutability
};

int main() {
    const User u(1, "Emma");  // object is const

    std::cout << u.getId() << std::endl;
    std::cout << u.getName() << std::endl;

    // u.name = "Tom";     // ERROR: private + const object
    // u.setName("Tom");   // ERROR: no setter exists
}


/*
run:

1
Emma

*/

 



answered 17 hours ago by avibootz
0 votes
#include <iostream>

// Immutable using const fields
// const fields — deep immutability

class User {
private:
    const int id;        // cannot change after construction
    const std::string name;   // cannot change after construction

public:
    User(int id, std::string name) : id(id), name(name) {}

    int getId() const { return id; }
    std::string getName() const { return name; }
};

int main() {
    User u(42, "Tom");

    std::cout << u.getId() << std::endl;
    std::cout << u.getName() << std::endl;

    // u.id = 6;       // ERROR: id is const
    // u.name = "Emma"; // ERROR: name is const
}



/*
run:

42
Tom

*/

 



answered 16 hours ago by avibootz
edited 16 hours ago by avibootz
0 votes
#include <iostream>

// Prevent modification by deleting setters
// deleted setters — prevent mutation by design

class User {
private:
    int id;
    std::string name;

public:
    User(int id, std::string name) : id(id), name(name) {}

    int getId() const { return id; }
    std::string getName() const { return name; }

    // Delete setter → cannot be called
    void setName(std::string) = delete;
};

int main() {
    User u(10, "Emma");

    std::cout << u.getName() << std::endl;

    // u.setName("Tom");  // ERROR: function is deleted
}



/*
run:

Emma

*/

 



answered 16 hours ago by avibootz
edited 16 hours ago by avibootz
0 votes
#include <iostream>

// Immutable references (const &)
// const references — safe read‑only parameters

void printUser(const std::string& name) {
    // name cannot be modified
    std::cout << name << std::endl;

    // name[0] = 'X';  // ERROR: cannot modify const reference
}

int main() {
    std::string s = "Emma";
    
    printUser(s);
}



/*
run:

Emma

*/

 



answered 16 hours ago by avibootz
0 votes
#include <iostream>

// Immutable class with dynamic allocation
// immutable classes — functional‑style immutability

class User {
private:
    const int id;
    const std::string name;

public:
    User(int id, std::string name) : id(id), name(name) {}

    int getId() const { return id; }
    std::string getName() const { return name; }
};

int main() {
    const User* u = new User(7, "Emma");

    std::cout << u->getId() << std::endl;
    std::cout << u->getName() << std::endl;

    // u->id = 99;       // ERROR: const field
    // u->name = "Tom";  // ERROR: const field

    delete u;  // safe cleanup
}



/*
run:

7
Emma

*/

 



answered 16 hours ago by avibootz
...