Templated Methods
I wasn't aware that one can do something like this in C++:
Ptr< MyClass > myObj = factory->Create< MyClass >(MyClass::RTTI);
I only saw this in some XNA code examples, and took it for something that's specific to C# generics. Turns out it works in C++ as well. When working with smart pointers, this is pretty nice, because automatic pointer casting doesn't work between smart pointers of different types. So the alternative to the above code would be (assuming the Create() method returns a Ptr<>):
Ptr< MyClass > myObj = (MyClass*) factory->Create().get();
Pretty ugly with the required cast and the .get() method...
In some cases, templated method calls may also save an implicit hidden construction of a smart pointer object. I'm not completely sold yet however:
The bad:
Nonetheless C++ never stops to amaze me, in some sort of scary way...
Ptr< MyClass > myObj = factory->Create< MyClass >(MyClass::RTTI);
I only saw this in some XNA code examples, and took it for something that's specific to C# generics. Turns out it works in C++ as well. When working with smart pointers, this is pretty nice, because automatic pointer casting doesn't work between smart pointers of different types. So the alternative to the above code would be (assuming the Create() method returns a Ptr<>):
Ptr< MyClass > myObj = (MyClass*) factory->Create().get();
Pretty ugly with the required cast and the .get() method...
In some cases, templated method calls may also save an implicit hidden construction of a smart pointer object. I'm not completely sold yet however:
The bad:
- it seems to be an unusual construct, haven't seen this yet in C++ code, maybe some compilers choke on this
- templated methods MUST be inline, and cannot be virtual, so it maybe that the mechanism cannot be used everywhere where it would make sense
- it's a perfect fit for smart pointers
- it may produce more efficient code (and should never produce less efficient code)
Nonetheless C++ never stops to amaze me, in some sort of scary way...