What do you mean by operator precedence in c plus plus?

1 answer

Answer

1266777

2026-05-07 06:06

+ Follow

Java
Java

Operator overloading is a mechanism where a C++ developer can assign a special meaning to an operator (such as + or -, for example). This is useful, because it allows developers to create classes that can interact with other classes or primitives naturally, using the same syntax as you'd expect when working with primitives. For example, using operator overloading allows cin and cout to work "magically":

cout << "Hello World" << endl;

Even though it appears that these are just "shift operations", they take on the special meaning of outputting (for cout) and inputting (for cin) without resorting to a more cumbersome:

cout.print("Hello World\n");

This short example doesn't illustrate the savings that this syntax allows; a string of 20 variables would very nicely outline how the shift operators streamline cout.

Unfortunately, this usage is considered controversial, because it allows developers to use odd syntax. Consider the following:

Vehicle car = new Vehicle();

Velocity speed = new Velocity(15);

car += speed;

This is legal syntax with operator overloading. While it might make sense to some developers, others less familiar with a library might need to dig down into the code to make sure that the effect is what the developer intended. This can slow down development and debugging. It is this controversial usage that has prompted future languages, such as Java, to specifically exclude such functionality in an attempt to keep code as legible as possible.

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.