CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    Join Date
    Apr 2005
    Posts
    15

    Can operator= assign const member?

    Code:
    Class A {
    public:
      A& operator=(const A& rhs);
    private:
      const int i;
      int j;
    };
    
    A& A::operator=(const A& rhs) {
      j = rhs.j;  // this is OK
      i = rhs.i;  // compiler error! how can I make this work?
    }
    
    // Not shown: operator= will check for assignment to self
    // and return *this, like all good assignment operators.
    Thanks for any help!

  2. #2
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Can operator= assign const member?

    What's the purpose of assigning const member, in your sense?
    You also did not have constructor in class that assigns const data member.

    Anyways, you can use C-Style casting or const_casting to do that:
    Code:
    A& A::operator=(const A& rhs) {
      j = rhs.j;  // this is OK
      *(const_cast<int*>(&i)) = rhs.i;  // compiler error! how can I make this work?
    }
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Can operator= assign const member?

    Use const_cast operator.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Apr 2005
    Posts
    15

    Re: Can operator= assign const member?

    I need assignment operator so that I can use in STL containers (std::vector for now). But as I'm typing this, I'm thinking that maybe I should just use containers of pointers (A*). That's probably the more efficient way to go, anyway.

    You're right, I didn't show my ctors. Those were easier to write, initializing the const member in the member initialization list.

    So, I'll try containers of pointers, and use your const_cast suggestions if I need to.

    Thanks

  5. #5
    Join Date
    Sep 2004
    Posts
    519

    Re: Can operator= assign const member?

    If you really need to have const members then pointers are the way to go as one should always strive to avoid casting.

    But raw pointers doesn't work to well with the standard containers and algorithms. What you could do is using a reference counted smart pointer such as boost::shared_ptr (http://www.boost.org/libs/smart_ptr/smart_ptr.htm).

    Code:
    std::vector< boost::shared_ptr<A> > my_vector;

  6. #6
    Join Date
    Mar 2003
    Location
    Germany, K-Town
    Posts
    578

    Re: Can operator= assign const member?

    This is an interesting question.
    Is that really the way to go? Using const_cast<> sounds like the quick and dirty way. It also breaks the class, because you can abuse the operator= to assign to a const member. That doesn't sound like a good idea.
    On the other hand, since STL containers are built around object copies, there has to be a cleaner solution, no?
    - Matthias

    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." - Bjarne Stroustrup

  7. #7
    Join Date
    Mar 2003
    Location
    Germany, K-Town
    Posts
    578

    Re: Can operator= assign const member?

    Sorry, missed your post. Using pointers sounds like a reasonable solution, though you will run into trouble when using STL algorithms, because they will be invoked on the pointers, not on the pointees. There are several solutions to this of course, but one should keep it in mind.
    - Matthias

    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." - Bjarne Stroustrup

  8. #8
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Can operator= assign const member?

    This works in VC7.0:
    Code:
       #include <utility>
       #include <iostream>
       
       using namespace std;
       
       class foo
       {
       public:
       	int j;
       	const int i;
       
       	foo() : j(1), i(10) { }
       	foo(int j_, int i_) : j(j_), i(i_) {}
       	foo(const foo& f) : j(f.j), i(f.i) {}
       	foo& operator=(const foo& rhs)
       	{
       		foo temp(rhs);
       		swap(temp);
       		return *this;
       	}
       
       private:
       	void swap(foo& other)
       	{
       	 std::swap(j, other.j);
       	    std::swap(const_cast<int&>(i), const_cast<int&>(other.i));
       	}
       };
       
       int main()
       {
       	foo f1(3,2);
       	foo f2;
       
       	f2 = f1;
       
       	cout << "f2 = (" << f2.j << ", " << f2.i << ")" << endl;
       
       	int t;
       	cin >> t;
       }
    Now, how about doing it when the class contains a reference variable?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  9. #9
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: Can operator= assign const member?

    Doesn't that cause undefined behavior (using const_cast and then modifing the member)?
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  10. #10
    Join Date
    Apr 2005
    Posts
    15

    Re: Can operator= assign const member?

    Not sure that I see the difference between Graham's code and the prior suggestion to const_cast; seems they both have the same result. In fact, it might be considered more inefficient because of constructing the extra object "temp".

    As I've thought about this more today, I'm resigning myself to the fact that your only opportunity to have control over const and reference members is during initialization. Once the object is constructed and you can call the assignment operator, it's too late to make any changes. Thus, if you want to identical copies of an object you must use the copy ctor. I think this is the only consistent answer to my question about const ints and Graham's question about references.

    Of course no assignment operator precludes putting the objects directly into an STL container, but you can always use containers of pointers (as long as you're careful).

  11. #11
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Can operator= assign const member?

    I also disregard with the post given by Graham. It does not matter if it works in VC7 and not in VC6, but it complicates the problem. All in all, we need to use 'const_cast' operator, whether we use '=' or 'std::swap'.

    And yes, why "swapping" at all?? We need to copy from source to destination, not exchanging them..!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  12. #12
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: Can operator= assign const member?

    Quote Originally Posted by Ajay Vijay
    And yes, why "swapping" at all?? We need to copy from source to destination, not exchanging them..!
    Exception safety. If you changed j, and then the modification of i threw an exception (it will of course not do that in this example, but might if i was an user-defined type), the object would be in an undefined state. (bad) Graham's version doesn't suffer from that problem.
    Insert entertaining phrase here

  13. #13
    Join Date
    Apr 2005
    Posts
    15

    Re: Can operator= assign const member?

    I like the idea of exception safety, but sorry, I don't see it in Graham's code. If an exception occurs in foo::swap things could still be left in a bad state.

    To implement exception safety, seems there should be a mechanism to catch the exception and undo partial changes.

  14. #14
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Re: Can operator= assign const member?

    Quote Originally Posted by n.marler
    I like the idea of exception safety, but sorry, I don't see it in Graham's code. If an exception occurs in foo::swap things could still be left in a bad state.

    To implement exception safety, seems there should be a mechanism to catch the exception and undo partial changes.

    The entire idea behind the swap() technique is that swap() doesn't throw. However, using const_cast to modify a const value will yield undefined behaviour, I think.

    Semantically, I'd say that objects with const members are not assignable and thus cannot be safely put into standard containers.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  15. #15
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Can operator= assign const member?

    You could use the 'mutable' keyword on your const member. It only works for members though.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured