Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1061393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:28:03+00:00 2026-05-16T18:28:03+00:00

Imagine I have a class used to represent some trivial numerical data, like a

  • 0

Imagine I have a class used to represent some trivial numerical data, like a vector. I want this class to be immutable in terms of its members, but still support correct copy-assignment behaviour.

This is what the shell of this class might look like:

class Vector {
  public:
  Vector(float _x, float _y);

  private:
  const float x;
  const float y;
};

I would like to be able to assign a new value to a vector member within (for example) a class. An on-screen entity with a position, perhaps, represented using a non-const member of type Vector.

This ‘entity’ must be mutable itself (perhaps supporting movement), e.g. the internal Vector member used to store its position must change. Rather than modifying this member directly, I would rather replace it with an entirely new Vector instance, initialised with the modified values.

In a language such as C#, I would simply compute the new values for X and Y based on the current Vector instance, and assign a new Vector object initialised with these values to the member, discarding the previous value and letting the GC do the rest.

// Inside some class (C#/Java type)...
Vector position;
...
// Example function:
void Move(float _dX, float _dY) {
  this.position = new Vector(_dX + position.X, _dY + position.Y);
}

This is where my limited ability with C++ comes into play, because I am unsure of how to implement operator= in such a way that instances of this immutable type contained in dynamic memory do not simply ‘disappear’ and cause a leak.

So, I suppose, what I am asking is this:

Vector& operator=(const Vector& _rhs);
...
// In implementation file...
Vector& Vector::operator=(const Vector& _rhs) {
  // WHAT ON EARTH GOES IN HERE?!?!?
}

The Vector case is something I invented to get my point across, I can think of many sorts of types that should be immutable at the member level.

Am I wasting my time attempting to model immutable types?

Is there a better solution?

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:28:03+00:00Added an answer on May 16, 2026 at 6:28 pm

    When you type

    Vector position;
    

    in a class definition, you define an instance of a Vector class that will stay here forever. If that one is immutable, you’re screwed.

    In C#/Java, there is no way to do this. Instead you define references.

    If you want to do the same thing in C++, you need pointers or auto_pointers

    Vector* position;
    

    That way, you can change the instance to which position is pointing.

    You shouldn’t define the assignment operator of a immutable class. If you really need to, maybe you should make Vector mutable, and use const Vector when you need an immutable Vector. You will just need to add const qualifiers to method that are allowed with a const Vector.

    eg:

    class Vector {
        private:
        float x;
        float y;
        public:
        // allowed for const Vector
        // because of the const before the '{'
        float norm() const {
            return hypot(x,y);
        }
    
        Vector& operator=(const Vector& o) {
            x=o.x;
            y=o.y;
            return *this;
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, let's imagine I have an object like this: public class User { public
imagine something like this: import class B.*; interface A supports A.testSum { int sum(
Imagine you have class A which has code which runs as method M. And
Imagine I have a class Window with a member function show which causes the
Imagine I have a C++ class Foo and a class Bar which has to
Imagine I have an entity: public class MyObject { public string Name { get;
Imagine that I have a nice Deck class, in the best OO fashion. It
Imagine you have two views with code like the following: controller_a/a.html.erb <%= content_tag(:div) do
Imagine I have a cell that I want to be red if the value
Imagine you have 2 databases : xpto & zpto. I want to do a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.