Are variable names also operands?

Jun 14, 2019 at 12:57am
Is a variable name also classified as an operand, ie
 
double df = 8/4;

is df classified as a operand, or are just 8 and 4 the operands?
Jun 14, 2019 at 1:30am
df is an operand for the operator=
Jun 14, 2019 at 1:52am
Thank you for response. :)
Jun 14, 2019 at 5:02am
In double df = 8/4;, df is not an operand. (Note that the = is not an operator here.)

An operand is part of an expression.
In the expression 8/4, 8 and 4 are operands, / is an operator.
In the expression std::cout << df , std::cout and df are operands, << is an operator.

Jun 14, 2019 at 6:55am
What you wrote is initialization. It might look like you're using an operator but you're not. The = symbol is just part of the initialization syntax. Another way to write the initialization would have been double df(8/4);. This might look like a function call because it uses parentheses but it's not. It's just a different way to do initialization.

If you rewrite it as

1
2
double df;
df = 8/4;

then df is an operand to the assignment operator.
Last edited on Jun 14, 2019 at 7:03am
Jun 14, 2019 at 12:50pm
And there's the "new style" initialization I'm still getting used to double df {8/4}; or double df = {8/4};
Jun 14, 2019 at 1:19pm
are you saying that
double df = 3.14; //not an operator, special initialization something or other and the value is not assigned anywhere, perhaps compiled into its memory location directly, etc?
df = 2.718; //an operator, doing the normal assignment operation?

Jun 14, 2019 at 2:00pm
Consider the = in the dynamic (run-time) initialisation of the constant object c in
const int c = std::rand() ;


Hopefully, the confusion would go away.
Jun 14, 2019 at 2:11pm
Not really :(
taht looks to me like the compiler has to call rand, take that value, and move it to a memory location (assignment operator task). I see no difference in that and
c = rand();

for compile time inits, I do see it, sort of, but it seems like splitting hairs.
Last edited on Jun 14, 2019 at 2:13pm
Jun 14, 2019 at 2:20pm
jonnin, can you assign to a constant?
Jun 14, 2019 at 2:27pm
> I see no difference in that and c = rand();

That would give you two compile-time errors.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <cstdlib>

int main()
{
    static const int c ; // *** error: const int must be explicitly initialised

    
    c = std::rand() ; // *** error: cannot assign to const int
}

http://coliru.stacked-crooked.com/a/45c82b094caa7550
Jun 14, 2019 at 2:33pm
@jonnin: Do you see difference in:
1
2
void func( int* arr );
void func( int arr[] );

For the compiler does not.

Then again, compiler does differentiate between
= the initialization syntax and
= the assignment operator.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// my first program in C++
#include <iostream>

struct Real {
    Real( int x )
    : y{x}
    {
        std::cout << "initialization I\n";
    }

    Real( const Real& rhs )
    : y{rhs.y}
    {
        std::cout << "initialization R\n";
    }

    Real& operator= ( int x ) {
        y = x;
        std::cout << "assignment I\n";
        return *this;
    }

    Real& operator= ( const Real& rhs ) {
        y = rhs.y;
        std::cout << "assignment R\n";
        return *this;
    }

    int y {};
};

int main()
{
  Real answer = 42;
  Real other {42};
  Real third (42);
  Real four = {42};
  Real five = Real(42);
}

initialization I
initialization I
initialization I
initialization I
initialization I

Jun 14, 2019 at 3:23pm
Ok. I see, thanks.
I know you can't assign to const, I should have changed the variable name or used more words. Was a bad question/example for sure.

but I see it better with the objects. I was thinking 'ints' not 'objects' and getting myself confused. It probably does the same thing for ints at the compiled code level, but it can't for objects as assignment and construction-init could be very different.

thanks guys. I would have said the same as ne555 before this!
Last edited on Jun 14, 2019 at 3:27pm
Topic archived. No new replies allowed.