By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,192 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,192 IT Pros & Developers. It's quick & easy.

why assignment operator required, but not used?

P: 3
I have a class with const member variables. I want to put objects from that class in a vector. For adding objects to the vector, the compiler (g++ 4.0.1 on mac os x 10.4.8) requires me to provide a copy constructor and an assignment operator for that class. A copy constructor is no problem. An assigment operator is problematic, since there are const members, which can't be changed in the body of the assignment operator. So I wrote an empty assigment operator (which is a bad thing to exist), to make the compiler happy. But in the end it appears that this assignment operator is not invoked at all. Why does the compiler require such an operator, that actually can't exist because of the presence of const members, while it is not used?

Tanks in advance!

Example code:

Expand|Select|Wrap|Line Numbers
  1. /****************************************
  2. *
  3. * Problem(?): operator= for class with const member is not desirable. But in this code the compiler says it must be there.
  4. *
  5. \****************************************/
  6.  
  7. #include<vector>
  8. #include<iostream>
  9. using namespace std;
  10.  
  11. class A {
  12. private:
  13.     const int unchangable;
  14.     int changable;
  15.  
  16. public:
  17.     A(int unch, int ch);
  18.     A(const A& a);
  19.     A();
  20.     A& operator=(const A& a);
  21.  
  22.     int getUnchangable() const {return unchangable;}
  23.     int getChangable() const {return changable;}
  24. };
  25.  
  26.  
  27. A::A(int unch, int ch) : unchangable(unch), changable(ch) {
  28. }
  29.  
  30. A::A(const A& a) : unchangable(a.getUnchangable()), changable(a.getChangable()) {
  31. }
  32.  
  33. A::A() : unchangable(1), changable(2) {
  34. }
  35.  
  36. A& A::operator=(const A& a) {
  37.     cout << "In A::operator=" << endl;
  38.     return *this;
  39. }
  40.  
  41. int main() {
  42.     vector<A> aas;
  43.     aas.push_back(A(3, 4));
  44.  
  45.     cout << aas[0].getUnchangable() << " " << aas[0].getChangable() << endl;
  46.  
  47.     return 0;
  48. }
  49.  
Nov 2 '06 #1
Share this Question
Share on Google+
3 Replies


P: 73
U might have not used the code, but it is needed in compile.
like this:
Expand|Select|Wrap|Line Numbers
  1. a = 100;
  2. b = 2;
  3. if( a < b) {
  4.    a = a * b;
  5. }
  6.  
" * " is never used but it is needed in compile.
Nov 3 '06 #2

P: 3
U might have not used the code, but it is needed in compile.
Ok. I can accept that.

But in this case the strange thing is that is impossible to write a good assignment operator, but it is still required. I don't want that assignment operator to exist at all, because it doesn't do what it is supposed to do, which makes it dangerous.

So, the compiler requires to have somthing of which it can know that it is impossible to function properly.
Nov 3 '06 #3

P: 73
But as u are using stl u must follow some given rule of stl in your implementation. If u don't what to do this, please use assert, or u
may change your implementation.
Nov 7 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.