assignment operator

Hey i just started reading c++ today, and i was wondering if someone could tell me why we would need this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// assignment operator

#include <iostream>
using namespace std;

int main ()
{
  int a, b;         // a:?,  b:?
  a = 10;           // a:10, b:?
  b = 4;            // a:10, b:4
  a = b;            // a:4,  b:4
  b = 7;            // a:4,  b:7

  cout << "a:";
  cout << a;
  cout << " b:";
  cout << b;

  return 0;
}


Why can't we just write that a = 4 in the start?
sorry if this is a noobish question
 
int a = 4;


is completely valid and preferred to

1
2
int a;
a = 4;

Just to make it clearer.

in C++ you declare and define variables.When you use:

 
int a;


you are just declaring a as an integer, and you have to define it later,
but as jsmith said, it is perfectly valid to declare and define a variable in a single
statement, like:

 
int a = 4;


and in case you are wondering, you can also do this:

 
int a = 4, b = 7;


or if they are supposed to be initialized to the same value, even:

 
int a = b = 7;
Last edited on
Or you can make it look like object construction which is how I like to do it. This makes it clearer that the variable a is being constructed and initialized with a value of 4.
1
2
3
4
5
int a(4);
int a = 4; // equivalent to previous line but looks more like assignment

int a;  // object constructed but not initialized
a = 4; // value of 4 is assigned to the object. 


int a = b = 7; is invalid -- b is not declared.

The closest you can get to such a construct is the fugly int a = 7, b = a;. Personally, I think it is just better to declare each variable on its own line:
1
2
int a = 7;
int b = 7;
or
1
2
int a( 7 );
int b( 7 );


Hope this helps.

int a = b = 7; is invalid -- b is not declared.


My mistake, actually what you can do to assign the same value to
more than one variable is this:

1
2
int a, b;
a = b = 5;
you could do int a = int b = 7;
you could do int a = int b = 7;


That's not valid.
 
you could do int a = int b = 7; 


That does not compile with in my machine.

 
error: expected primary-expression before ‘int


It's illegal syntax. The only time you can do something close to that is if all variables are declared prior. For example, in a default constructor call you could do this:

a = b = c = d = 0;
Last edited on
oh my bad. thought it would work.
Remember = evaluates from right to left.
a = b = 5;
Since b = 5; returns the value of b, it can then be assigned to a a = (b = 5);.

It compiles if the compiler knows what it means.
It doesn't if the compiler have no idea of what it means.


Referring to blast3r's question
Why can't we just write that a = 4 in the start?
.
The answer that immediately comes to my mind is that
It demonstrates what the assignment operator does. For example, how you assign the value of a variable to another.
Last edited on
I know you can write

int a(4)

but surely int is a primative type and as such not an object, and therefore not contructed?

why can't we just write a = 4 at the start....


Well, you need to specify what type a is

so

int a = 4; is fine

as is

int a = 4, b = 7;

But this has been said before.


Thanks for the answers. I think you guys misunderstood my question, but that may be because i asked it in a wrong way. but wmheric got me the answer thanks.
Topic archived. No new replies allowed.