dreiti
dreiti

Reputation: 71

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?

Upvotes: 0

Views: 146

Answers (1)

Edgar Rokjān
Edgar Rokjān

Reputation: 17483

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.

Upvotes: 2

Related Questions