1

Is is possible to overload a reference cast in C++?

I've got code I can't touch in the format:

void someMethod(Parent& parentReference, ...){
    ...
    Child& child = static_cast<Child&>(parentReference);

(The class Child inherits directly and publicly from the class Parent)

I'd like to adjust the behavior of this cast - I can modify the class Child.
I've tried overloading the cast operator like so:

Parent::operator Child&(){
    ...

But this method never gets called.

I'm starting to wonder if this is even possible?

EDIT
Per R Sahu, I'm close to this scenario: https://timsong-cpp.github.io/cppwp/n3337/expr.static.cast#2

struct B { };
struct D : public B { };
D d;
B &br = d;

static_cast<D&>(br);            // produces lvalue to the original d object

Except that instead of simply assigning B &br = d;, br comes into the method as an argument, and is previously sent over the network (as NML).

This would be the scenario:

struct B { };
struct D : public B { 
   int a;
   int b
};

D d;
d.a = x;
d.b = y;

server.send(d); 

...

client.receive(msg);

receive(B& msg){

  D& msgD = static_cast<D&>(msg);

}

msgD.x and msgD.y come over the wire and are reconstructed properly. However, I would like to change the way they are reconstructed, without modifying the receive method. Is this possible?

fatman
  • 65
  • 6
  • 1
    If you can't modify `Parent`, then the compiler shouldn't even let you define `Parent::operator Child&()`. – Brian Bi Sep 19 '17 at 20:25
  • 1
    Possible duplicate of [Is it possible to overload the \*static\_cast\* operator?](https://stackoverflow.com/questions/8432651/is-it-possible-to-overload-the-static-cast-operator) – Fantastic Mr Fox Sep 19 '17 at 20:25
  • @Brain - yeah, that was a mistake - I've edited accordingly. I actually can modify Parent. – fatman Sep 19 '17 at 20:41
  • you need to modify reconstruction, so do so. usually it's called de-serialisation. or show us how it's done – Andriy Tylychko Sep 21 '17 at 14:51

1 Answers1

0

Given classes

struct Parent
{
};

struct Child : Parent
{
};

static_cast<Child&> will not use a user defined conversion function.

From 5.2.9 Static cast/2

An lvalue of type “cv1 B,” where B is a class type, can be cast to type “reference to cv2 D,” where D is a class derived from B, if a valid standard conversion from “pointer to D” to “pointer to B” exists , cv2 is the same cv-qualification as, or greater cv-qualification than, cv1, and B is neither a virtual base class of D nor a base class of a virtual base class of D. The result has type “cv2 D.”

and then there are the "otherwise" clauses in 5.2.9 Static cast/4 and 5.2.9 Static cast/5.

My interpretation of the above is that since Child is a sub-type of Parent, the clauses 4 and 5 above don't apply.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Then how come `Child child3 = static_cast(parent);` calls `Parent::operator Child&(){...` – fatman Sep 19 '17 at 20:45
  • 2
    @fatman, I updated the answer just a tad. `static_cast` is different than `static_cast`. – R Sahu Sep 19 '17 at 20:52
  • @R Sahu - thanks for the reply - it helped shine a light on exactly what I am trying to do. I've added more detail to my question above. – fatman Sep 21 '17 at 14:34