2

There are three ways I know of to create an immutable object.

Method 1: internally immutable class members, internally and externally unmodifiable.

#ifndef INTERNALLY_IMMUTABLE_HPP
#define INTERNALLY_IMMUTABLE_HPP

#include <string>

class internally_immutable
{
public:
    internally_immutable(const std::string & str)
    : str_(str)
    {
    }

    std::string get_str() const
    {
        return str_;
    }

private:
    const std::string str_;
};

#endif

Method 2: externally immutable class members, externally unmodifiable.

#ifndef EXTERNALLY_IMMUTABLE_HPP
#define EXTERNALLY_IMMUTABLE_HPP

#include <string>
#include <vector>

class externally_immutable
{
public:
    externally_immutable(const std::string & str)
    : str_(str)
    {
    }

    std::string get_str() const
    {
        return str_;
    }

private:
    std::string str_;
};

#endif

Method 3: type immutable, partially externally unmodifiable, as someone could bypass your typedef.

#ifndef TYPED_IMMUTABLE_HPP
#define TYPED_IMMUTABLE_HPP

#include <string>
#include <vector>

typedef const typed_mutable typed_immutable;

class typed_mutable
{
public:
    typed_mutable(const std::string & str)
    : str_(str),
    vec_()
    {
    }

    void set_str(const std::string & str)
    {
        str_ = str;
    }

    std::string get_str() const
    {
        return str_;
    }

private:
    std::string str_;
};

#endif

What are the pros and cons of each immutable type? Compiler optimizations, impediments, usage of each type... Are there other ways of creating immutable objects in C++? What is the recommended, or most common way to create these immutable classes in C++?

Francisco Aguilera
  • 3,099
  • 6
  • 31
  • 57
  • It doesn't make much sense to return `const` values from member functions. In any case, it doesn't impact the mutability of the object the member function is being called on. – juanchopanza Mar 08 '16 at 06:39
  • @juanchopanza it may not make sense from a user endpoint but what about a compilation endpoint? – Francisco Aguilera Mar 08 '16 at 06:41
  • I don't know what it means to make sense "from a compilation standpoint". – juanchopanza Mar 08 '16 at 06:42
  • @juanchopanza Any optimizations the compiler could make knowing the returned value won't be modified. – Francisco Aguilera Mar 08 '16 at 06:43
  • That is posing some restrictions (that can be easily bypassed) just in the hope of some optimizations. I would consider it only if it really is necessary. But it is a different topic to your question. – juanchopanza Mar 08 '16 at 06:45
  • @juanchopanza The third is partially immutable, because if a user bypasses your typedef they can use the class in an unexpected manner. This topic is relevant to my question i believe. – Francisco Aguilera Mar 08 '16 at 06:50
  • Now it is different because you just edited it to make it so. – juanchopanza Mar 08 '16 at 06:51
  • @juanchopanza woah. relax. I noticed I mis-copy pasted. This was the intention of my question, read the comment above which I did not edit. – Francisco Aguilera Mar 08 '16 at 06:52
  • 1
    Sorry about that. Going back to the `const` return values, that can actually work against you because it will disallow move semantics. In your example, you have two types that are efficiently movable. It is one more thing to consider. – juanchopanza Mar 08 '16 at 06:59
  • 1
    It makes no sense to return a const vector from a function, from any standpoint. – n. m. could be an AI Mar 08 '16 at 07:00
  • thank you, good things to know even if irrelevant to the question. I will edit the question to be correct in this sense. – Francisco Aguilera Mar 08 '16 at 07:04
  • I found this question on a different stackexchange that is kind of relevant in that they describe the difference between logical immutability vs immutability: http://programmers.stackexchange.com/questions/149555/difference-between-immutable-and-const – Francisco Aguilera Mar 08 '16 at 07:07
  • @juanchopanza this stack exchange question validates the importance of what you mentioned as const return types do not perform any kind of optimization http://stackoverflow.com/questions/27466642/what-kind-of-optimization-does-const-offer-in-c-c – Francisco Aguilera Mar 08 '16 at 07:27

1 Answers1

0

you can make the immutable class when will you make your constructor private in this case your derived class can't access base class. like as . class externally_immutable { private : externally_immutable(const std::string & str) : str_(str){} // to do const string str_; };

S Prakash
  • 1
  • 3