Silly question: what is the best way to pass this by value?
Background: Let’s use cars and car options. I used to have constructors such as Ford(FordOptions), Honda(HondaOptions). All cars derive from Car, all options from CarOptions. I used to have a car factory that gets passed a pointer to a CarOption, checks the type, produces the appropriate car, and returns a pointer to a generic Car.
I have now changed this so that CarOption has a virtual ProduceCar returning a Car, and every individual car option implements its particular production. So when I get a generic pointer to a car option, I just call CarOptions.ProduceCar and get a generic pointer to a car.
This is all fine, except in the implementation for say FordOption.ProduceCar I need to pass the FordOption instance to the Ford constructor. I can do this by passing “this” and changing the constructur for Ford to accept a pointer to FordOptions instead of FordOptions. However, I’d like it to take FordOptions by value, or at least by reference, but struggle to do so.
Long question for a simple answer I bet. Thanks.
You just have to dereference the
thispointer… And depending on the called method, you pass a reference (as in the example), or by value