Beginner's Question: Assignment of Objects with a constant data member possible?

Discussion in 'C++' started by CoolPint, Aug 13, 2003.

  1. CoolPint

    CoolPint Guest

    It seems to me that I cannot assign objects of a class which has a constant data
    member since the data member cannot be changed once the constructor calls are
    completed. Is this the way it is meant to be? Am I not suppose not to have any
    constant data member if I am going to have the assignment operator working for
    the class?

    Or am I missing something here and there is something I need to learn about?

    Clear, easy to understand explanation would be very much appreciated as I am
    just beginning to learn C++ features. Thank you in advance!
     
    CoolPint, Aug 13, 2003
    #1
    1. Advertising

  2. "CoolPint" <> wrote...
    > It seems to me that I cannot assign objects of a class which has a

    constant data
    > member since the data member cannot be changed once the constructor calls

    are
    > completed. Is this the way it is meant to be? Am I not suppose not to have

    any
    > constant data member if I am going to have the assignment operator working

    for
    > the class?
    >
    > Or am I missing something here and there is something I need to learn

    about?


    You can write your own assignment operator and do whatever you think
    is appropriate. I guess you need to learn about operator overloading.

    > Clear, easy to understand explanation would be very much appreciated as I

    am
    > just beginning to learn C++ features. Thank you in advance!


    What book are you reading? Does it have a chapter on operators? Does
    it talk about operator overloading for your custom types? If not,
    discard it and find a better one.

    class HasConstData
    {
    int const a;
    int b;
    public:
    HasConstData(int b) : a(42), b(b) {}
    HasConstData& operator=(HasConstData const& r)
    {
    b = r.b;
    return *this;
    }
    };

    int main()
    {
    HasConstData hcd1(1), hcd2(2);
    hcd1 = hcd2;
    }

    Victor
     
    Victor Bazarov, Aug 13, 2003
    #2
    1. Advertising

  3. CoolPint

    Jim Fischer Guest

    Re: Beginner's Question: Assignment of Objects with a constant datamember possible?

    CoolPint wrote:
    > It seems to me that I cannot assign objects of a class which has a constant data
    > member since the data member cannot be changed once the constructor calls are
    > completed. Is this the way it is meant to be? Am I not suppose not to have any
    > constant data member if I am going to have the assignment operator working for
    > the class?
    >
    > Or am I missing something here and there is something I need to learn about?
    >
    > Clear, easy to understand explanation would be very much appreciated as I am
    > just beginning to learn C++ features. Thank you in advance!


    You need to explicitly write the assignment operator and copy
    constructor methods for your class.

    <example>
    class X {
    private:
    int a_;
    const int b_;
    public:
    // ctor
    X(int a, int b) : a_(a), b_(b) { }
    // copy ctor
    X(const X& x) : a_(x.a_), b_(x.b_) { }
    // assignment
    X & operator=(const X& x) {
    a_ = x.a_;
    return *this;
    }
    };

    int main()
    {
    X x1(1,2), x2(3,4);
    X x3(x1); // copy ctor
    // x3.{a_==1, b_==2}

    // x2.{a_==3, b_==4}
    x2 = x3; // assignment
    // x2.{a_==1, b_==4}
    }
    </example>

    --
    Jim

    To reply by email, remove "link" and change "now.here" to "yahoo"
    jfischer_link5809{at}now.here.com
     
    Jim Fischer, Aug 13, 2003
    #3
  4. "CoolPint" <> wrote in message
    news:...
    > It seems to me that I cannot assign objects of a class which has a
    > constant data member since the data member cannot be changed once the
    > constructor calls are completed. Is this the way it is meant to be? Am I
    > not suppose not to have any constant data member if I am going to have the
    > assignment operator working for the class?
    >
    > Or am I missing something here and there is something I need to learn
    > about?


    You mean, you have a class like this

    class Foo
    {
    const int unchangeableValue;
    int changeableValue;
    public:
    Foo (int, int);
    };

    Foo::Foo (int value1, int value2) :
    unchangeableValue(value1), changeableValue(value2)
    {
    }

    and you're trying to do an assignment like this?

    Foo foo1(1, 2);
    Foo foo2(3, 4);
    foo1 = foo2;

    This is called "copy assignment," because both objects in the assignment are
    of the same type. The default method used for copy assignment is called
    "memberwise copy." In this method, each member of one object is simply
    assigned to the corresponding member of the other object. So this statement

    foo1 = foo2;

    leads to code that assigns foo2.unchangeableValue to foo1.unchangeableValue,
    and assigns foo2.changeableValue to foo1.changeableValue. The problem, of
    course, is that you can't assign foo2.unchangeableValue to
    foo1.unchangeableValue, because unchangeableValue is defined to be const.

    One solution would be to define unchangeableValue to be non-const. But that
    may not be what you want.

    Another solution might be to define unchangeableValue as a static member of
    Foo. Static members aren't involved in copy assignment. But the side
    effect would be that every instance of Foo would have the same
    unchangeableValue. This may or may not be acceptable to you.

    Another solution would be to "overload" the copy assignment operator for
    Foo. In other words, substitute your own copy-assignment operation for the
    default memberwise-copy operation.

    You overload the assignment operator by adding a new member function to Foo.
    The name of this function is "operator=":

    class Foo
    {
    int const unchangeableValue;
    int changeableValue;
    public:
    Foo (int, int);
    Foo &operator= (Foo const &);
    };

    // constructor
    Foo::Foo (int value1, int value2) :
    unchangeableValue(value1), changeableValue(value2)
    {
    }

    // copy assignment
    Foo &Foo::eek:perator= (Foo const &other)
    {
    // we're ignoring unchangeableValue
    changeableValue = other.changeableValue;
    return *this;
    }

    Now this statement

    foo1 = foo2;

    leads to this function call:

    foo1.operator=(foo2);

    Since operator= doesn't attempt to assign to unchangeableValue, it's legal.

    Hope that helps. Let us know if you have further questions.

    Regards,

    Russell Hanneken
     
    Russell Hanneken, Aug 13, 2003
    #4
  5. CoolPint

    CoolPint Guest

    Thank you Russell. You clearly explained what I was asking about.
    While I was pondering with the situation, I came up with similar
    solutions to yours including overloaded assignment operator which
    doesn't assign the constant variable.

    So is this the feature of the language which is meant to be like this?
    I see that alternative solutions can be formed but what I am trying to
    learn here the features of the language and possible situations where
    the features can be useful. With regard to assignment of objects with
    a constant data member,
    I guess that's how it is designed to be.

    For example,
    class Person {
    const unsigned reg_number;
    char *name;
    Date birthday;
    };

    If the application requires the registration number to be set just
    once and to be made constant after that, the assignment operator
    should not be allowed, I guess and the feature seems to make sense...





    "Russell Hanneken" <> wrote in message news:<n_v_a.3715$>...
    > "CoolPint" <> wrote in message
    > news:...
    > > It seems to me that I cannot assign objects of a class which has a
    > > constant data member since the data member cannot be changed once the
    > > constructor calls are completed. Is this the way it is meant to be? Am I
    > > not suppose not to have any constant data member if I am going to have the
    > > assignment operator working for the class?
    > >
    > > Or am I missing something here and there is something I need to learn
    > > about?

    >
    > You mean, you have a class like this
    >
    > class Foo
    > {
    > const int unchangeableValue;
    > int changeableValue;
    > public:
    > Foo (int, int);
    > };
    >
    > Foo::Foo (int value1, int value2) :
    > unchangeableValue(value1), changeableValue(value2)
    > {
    > }
    >
    > and you're trying to do an assignment like this?
    >
    > Foo foo1(1, 2);
    > Foo foo2(3, 4);
    > foo1 = foo2;
    >
    > This is called "copy assignment," because both objects in the assignment are
    > of the same type. The default method used for copy assignment is called
    > "memberwise copy." In this method, each member of one object is simply
    > assigned to the corresponding member of the other object. So this statement
    >
    > foo1 = foo2;
    >
    > leads to code that assigns foo2.unchangeableValue to foo1.unchangeableValue,
    > and assigns foo2.changeableValue to foo1.changeableValue. The problem, of
    > course, is that you can't assign foo2.unchangeableValue to
    > foo1.unchangeableValue, because unchangeableValue is defined to be const.
    >
    > One solution would be to define unchangeableValue to be non-const. But that
    > may not be what you want.
    >
    > Another solution might be to define unchangeableValue as a static member of
    > Foo. Static members aren't involved in copy assignment. But the side
    > effect would be that every instance of Foo would have the same
    > unchangeableValue. This may or may not be acceptable to you.
    >
    > Another solution would be to "overload" the copy assignment operator for
    > Foo. In other words, substitute your own copy-assignment operation for the
    > default memberwise-copy operation.
    >
    > You overload the assignment operator by adding a new member function to Foo.
    > The name of this function is "operator=":
    >
    > class Foo
    > {
    > int const unchangeableValue;
    > int changeableValue;
    > public:
    > Foo (int, int);
    > Foo &operator= (Foo const &);
    > };
    >
    > // constructor
    > Foo::Foo (int value1, int value2) :
    > unchangeableValue(value1), changeableValue(value2)
    > {
    > }
    >
    > // copy assignment
    > Foo &Foo::eek:perator= (Foo const &other)
    > {
    > // we're ignoring unchangeableValue
    > changeableValue = other.changeableValue;
    > return *this;
    > }
    >
    > Now this statement
    >
    > foo1 = foo2;
    >
    > leads to this function call:
    >
    > foo1.operator=(foo2);
    >
    > Since operator= doesn't attempt to assign to unchangeableValue, it's legal.
    >
    > Hope that helps. Let us know if you have further questions.
    >
    > Regards,
    >
    > Russell Hanneken
    >
     
    CoolPint, Aug 14, 2003
    #5
  6. CoolPint

    osmium Guest

    CoolPint writes:

    > It seems to me that I cannot assign objects of a class which has a

    constant data
    > member since the data member cannot be changed once the constructor calls

    are
    > completed. Is this the way it is meant to be? Am I not suppose not to have

    any
    > constant data member if I am going to have the assignment operator working

    for
    > the class?
    >
    > Or am I missing something here and there is something I need to learn

    about?
    >
    > Clear, easy to understand explanation would be very much appreciated as I

    am
    > just beginning to learn C++ features. Thank you in advance!


    Based on the amount of time that has elapsed since you made your post, I
    think most others are in the same boat I am, they don't understand your
    question. How about posting some code that illustrates your
    problem/concern/misapprehension?
     
    osmium, Aug 15, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim
    Replies:
    2
    Views:
    1,510
  2. Fraser Ross
    Replies:
    4
    Views:
    1,059
    Fraser Ross
    Aug 14, 2004
  3. Replies:
    25
    Views:
    693
    Steve Holden
    May 18, 2006
  4. Jason
    Replies:
    2
    Views:
    521
    Jonathan Mcdougall
    May 13, 2006
  5. G G
    Replies:
    3
    Views:
    95
    Ben Bacarisse
    Apr 20, 2014
Loading...

Share This Page