0

Java has a construct that allows a method to call itself via a "this()" reference. The name of this convention escapes me at the moment. EDIT: Known as Constructor Delegation as pointed out below. Will only work on constructors, not methods in general as I have mistaken.

Does C++ with all its pointery goodness provide a reference to itself, but not specifically itself?

For example, I have a function with multiple overloads and the like.

void DoesSomething(){
    this->(default1, default2);
}

void DoesSomething(int myValue1){
    this->(myValue1, default2);
}

void DoesSomething(int myValue1, int myValue2){
    //Do stuff
}
Daniel Park
  • 3,903
  • 4
  • 23
  • 38
  • Functions do not have an implicit “this” pointer. So no. Use function name instead. –  Jun 03 '13 at 16:46
  • 1
    Can we say "infinite recursion" ... :) – Nikolai Fetissov Jun 03 '13 at 16:47
  • Something like this would conflict with the C++ function call operator - `returntype operator()(parameters);` – Captain Obvlious Jun 03 '13 at 16:49
  • 1
    It is easy enough to say the next best thing is just to use the function name itself. Are there any benefits to using a keyword this way? – Daniel Park Jun 03 '13 at 16:49
  • 1
    Note that the particular example here would be more easily handled using the default argument construct `void DoesSomething(int myValue1=default1, int myValue2=default2)`. I assume you are interested in cases where that doesn't work or in understanding the abstract point, eyes? – dmckee --- ex-moderator kitten Jun 03 '13 at 16:51

3 Answers3

2

You need to specify the function name explicitly:

void DoesSomething(){
    this->DoesSomething(default1, default2);
}

void DoesSomething(int myValue1){
    this->DoesSomething(myValue1, default2);
}

void DoesSomething(int myValue1, int myValue2){
    //Do stuff
}

There is no implicit knowledge of the other function with a given name by context, like you're describing.

Note that in Java, this only works for constructors, and is called Constructor Chaining. It does not handle this for methods in general.

C++11 also adds support for this same concept (as delegating constructors), though the syntax is different than Java, C#, and other languages with this concept.

For constructors, in C++11, you can write:

SomeType() : SomeType(42) {}

SomeType(int arg)
{
   // ...
Community
  • 1
  • 1
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
1

That code needs to be modified to:

void DoesSomething(){
    this->DoesSomething(default1, default2);
}

void DoesSomething(int myValue1){
    this->DoesSomething(myValue1, default2);
}

void DoesSomething(int myValue1, int myValue2){
    //Do stuff
}

or to

void DoesSomething(){
    DoesSomething(default1, default2);
}

void DoesSomething(int myValue1){
    DoesSomething(myValue1, default2);
}

void DoesSomething(int myValue1, int myValue2){
    //Do stuff
}

because 'this' is not really needed to be explicitely stated in the code. What does exist in c++ is something called a function object like described here : http://www.cprogramming.com/tutorial/functors-function-objects-in-c++.html

Philip Stuyck
  • 7,344
  • 3
  • 28
  • 39
0

this points to the object on which the member function was called. I've seen it most often used when a function needs to return a reference to itself, e.g., operator()=; when overloading this operator, you will often also see something like the following to prevent self-assignment

foo& foo::operator=(const foo& rhs ){
  if (  this != &rhs ) { 
      //do something
  }
  return *this;
}

I think this answers your question??

reference: http://msdn.microsoft.com/en-us/library/y0dddwwd.aspx

ash
  • 3,354
  • 5
  • 26
  • 33
  • Exception-safe copy-assignment doesn't need a self-reference check. – Mooing Duck Jun 03 '13 at 17:17
  • Well, I added it to point out another usage of the "this" pointer. But aside from that, it's a simple check that can prevent future headache. – ash Jun 03 '13 at 18:02
  • The places I most commonly see `this` are `return *this`, and for accessing members of a parent class from a derived class template: http://ideone.com/6sFNjb. If I see a copy assignment with `if (this!=&rhs)`, I immediately assume the function is not exception safe. (For instance, the sample you linked to is not exception safe) – Mooing Duck Jun 03 '13 at 19:20