Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A valid C++ statement?

Tags:

c++

I couldn't find it online, so I thought I should ask here. Will

arr[0]++;

behave the same as

arr[0] = arr[0] + 1;

?

like image 549
Roguebantha Avatar asked Dec 20 '22 02:12

Roguebantha


2 Answers

For an integer it will.

But it depends on the type of arr.

like image 111
Yochai Timmer Avatar answered Jan 06 '23 20:01

Yochai Timmer


In this case, yes. But, in other situations, no, it will not.

In general, with an array of object or number, it will call the post-increment operator, which can be different from a regular pre-increment operator--although post-increment may appear to just increment alone, in reality, it will increment the actual object BUT it will return a copy of the unincremented object. Beware of this small technicality.

Also, note that user-defined operator overloads may not follow these semantics at all.

like image 42
CinchBlue Avatar answered Jan 06 '23 19:01

CinchBlue