0

Struggling to understand this syntax that I've run across in open source code:

/// cast *this into an gpstk::RinexNavData.
/// @throw if the record is invalid or not an ephemeris (isNav()==false)
operator RinexNavData() throw(gpstk::Exception);

/// cast *this into a gpstk::RinexObsData
/// @throw if the record is invalid or not an observation (isObs()==false)
operator RinexObsData() throw(gpstk::Exception);

If I interpret the comment correctly, it is changing the type of the object via the "this" pointer. But this appears to be done via an operator? Can't hit on a good web search that involves the keyword "this". Looking for a reference or explanation on how this use of "operator" works. Web search of C++ operator doesn't lead to anything like this, that I've found so far.

cas206
  • 3
  • 2

1 Answers1

3

Don't overthink the use of this here; *this just means "the current object", so the programmer is using a shorthand to describe what the operator does.

Indeed, like any conversion operator, it takes the current object and provides a means to convert it to a different type.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Minor nitpick: `this` is not converted. A new object is created (from `this`) via the cast operator, but I would expect `this` to remain unchanged. That in turn begets the question why the operator is not marked `const`, but that's a new discussion. – Ulrich Eckhardt Feb 28 '18 at 12:19
  • @UlrichEckhardt: `this` is certainly not converted. In C++ parlance, though, `*this` is - making a copy is part of the conversion procedure (it happens every time you make a value cast!). Indeed you cannot transform an object to a different type in-place. – Lightness Races in Orbit Feb 28 '18 at 12:23