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.
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.
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?
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.
#include <iostream>
#include <cstdlib>
int main()
{
staticconstint c ; // *** error: const int must be explicitly initialised
c = std::rand() ; // *** error: cannot assign to const int
}
// 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
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!