简体   繁体   中英

How to use alias names for variables?

I want to improve my code, is there an other way to assingn an alias name for
parent->values[i-1] ?
The isssue is that values [i - 1] isn't really intention revealing, however the reference costs some runtime.

if (belegt < k) {
     if (prev && prev->belegt > k && prev->parent == parent) {
          for (size_t i = 0; i <= parent->belegt; i++) {
               if (parent->leafptr[i] == this) {
                    Key &above = parent->values[i - 1];
                    Key &previous = prev->values[prev->belegt - 1];

                    add(above, leafptr[0]);

                    leafptr[0] = prev->leafptr[prev->belegt];
                    leafptr[0]->setParent(this);

                    parent->replace_rekursiv(above, previous);

                    prev->belegt--;
                    break;
                }
           }

I thought of something like

using above = previous = prev->values[prev->belegt - 1];

but that won´t do the trick?

is there any other way to assign an alias name for parent->values[i-1]?

As far as I know, the only one good way is to create a reference and work with this reference as you already did:

Key &above = parent->values[i - 1];

Your assumption about run time cost seems to be incorrect - any sane compiler will optimize this code away, so you might not bother.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM