0

My constructor:

bnf::bnf(string encoded)
{
    this->encoded = encoded;
}

copies the string data to a member. (Or does it..?)

I will have a recursive decode method, but would like to avoid writing this->encoded all the time.

How can I validly and simply create an alias/reference to the member within a method?

Will this have overhead best avoided?

user76568
  • 456
  • 4
  • 16

2 Answers2

1

You can just pass in a different named parameter. This is assuming that encoded is a private string member of your bnf class

bnf::bnf(string en)
{
    encoded = en;
}

In your other functions, you still don't need to write this if you don't want to:

void bnf::printCode(){
     cout << encoded << endl;
}

Assuming your class looks like this:

class bnf{
    public:
         bnf(string en};
         void printCode();
         //<some other functions>
    private:
         string encoded;
}
stack smasher
  • 443
  • 3
  • 10
  • To be complete, you can use the same name if using initialize lists in constructors. – ibizaman Mar 08 '14 at 00:23
  • I know I can. It's not what I meant. I mean, in a different method, how can I `enc = this->encoded` **without** copying the data? And maintaining the member's state? – user76568 Mar 08 '14 at 00:25
  • I'm not sure what you are asking. What do you mean by `enc = this->encoded` without copying the data? – stack smasher Mar 08 '14 at 00:26
  • @stakSmashr I mean I just want `enc` to stand for the member, just to avoid writing `this` all the time :-). But when I think about it again, It's bad coding.. – user76568 Mar 08 '14 at 00:35
  • @Dror it's not bad coding, just style difference. There's nothing wrong with not using the `this` pointer. You can always refer to a private member variable of a class by itself without using `this` – stack smasher Mar 08 '14 at 00:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49277/discussion-between-staksmashr-and-dror) – stack smasher Mar 08 '14 at 00:43
1

There is nothing wrong with what you're doing now. It's expressive, clear and correct. Don't try to ruin it.

If you're worried about "overhead" with using the this pointer, don't: it's already as efficient as it could possibly be. There is literally no way to make it faster.

If your question is slightly wrong and all you want to do is mention a member variable inside a member function, then:

struct MyClass
{
   int x;
   void myFunction();
};

void MyClass::myFunction()
{
   this->x = 4;
}

The function is equivalent to:

void MyClass::myFunction()
{
   x = 4;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • It's not what I meant. But you are right with the first sentence I think. (Just stick with `this->encoded`) – user76568 Mar 08 '14 at 00:31
  • The overhead I was talking about is regarding if I'd explicitly create a reference to it in the beginning of a recursive method. – user76568 Mar 08 '14 at 00:42
  • @Dror: Not sure what you mean. Whether you create a reference or not, though, you have to access the member, and going through the `this` pointer is as good as any other method. If it can be optimised, your compiler will optimise it. – Lightness Races in Orbit Mar 08 '14 at 00:46