Using std::unique_ptr for down cast
What is the correct usage of std::unique_ptr for converting to base class a derived class ?
std::unique_ptr<Base*> SomeMethod()
{
return std::unique_ptr<Derived*>(new Derived());
return std::make_unique<Base*>(new Derived());
}
?
C++
-
David Lowndes 4,716 Reputation points
2020-12-09T16:22:16+00:00 You probably want:
std::unique_ptr<Base> SomeMethod() { return std::unique_ptr<Derived>( new Derived() ); }
-
Flaviu_ 991 Reputation points
2020-12-09T16:31:11.39+00:00 I have tried:
extern "C++" __declspec(dllexport) std::unique_ptr<Base> __cdecl SomeMethod(const char* type) { if (cond1) return std::unique_ptr<Derived1>(new Derived1()); if (cond2) return std::unique_ptr<Derived2>(new Derived2()); return nullptr; }
and in client app:
FACTORY pFactory1 = (FACTORY)GetProcAddress(hInstance, "SomeMethod"); if (!pFactory1) { std::cout << "Could not get GetProcAddress for SomeMethod\n"; }
and pFactory1 is always NULL.
-
Flaviu_ 991 Reputation points
2020-12-09T16:32:37.153+00:00 BTW, this code is working well without std::unique_ptr, it's just for testing purpose.
-
David Lowndes 4,716 Reputation points
2020-12-09T16:45:13.247+00:00 As it's a C++ method, its name won't be "SomeMethod", it'll be decorated.
Do you need to do dynamic (run-time) linking? -
David Lowndes 4,716 Reputation points
2020-12-09T17:00:38.417+00:00 or:
return std::make_unique<Derived>( Derived() );
-
Flaviu_ 991 Reputation points
2020-12-09T17:19:13.603+00:00 Yes, will be used for dynamic linking, as DLL, if this is what you mean.
-
Flaviu_ 991 Reputation points
2020-12-09T17:20:52.467+00:00 Is not working without new, got errors instead
return std::make_unique<Derived>( Derived() ); // got errors return std::make_unique<Derived>( new Derived() ); // no error
-
RLWA32 45,571 Reputation points
2020-12-09T17:33:41.853+00:00 You can use -
return std::make_unique<Derived>();
-
RLWA32 45,571 Reputation points
2020-12-09T18:08:30.69+00:00 In this context dynamic-linking refers to using GetProcAddress to reference functions from the DLL at run-time instead of having the consumer of DLL functions link with the DLL's import library so that the linker can resolve the references at build-time.
-
David Lowndes 4,716 Reputation points
2020-12-09T18:14:36.32+00:00 I was wondering why you were using GetProcAddress rather than keeping it simple by using dllimport/dllexport.
-
Flaviu_ 991 Reputation points
2020-12-09T18:43:48.417+00:00 Because that was the request, to use GetProcAddress. And everything is going fine if I don't use std::unique_ptr, but for my knowledge I tried with no success to use std::unique_ptr instead of raw pointer.
-
Igor Tandetnik 1,106 Reputation points
2020-12-11T15:23:54.6+00:00 To export a C++ function under an undecorated name, use a DEF file.
You can use Dependency Walker to see the actual name the function got exported under. This is the exact name you need to pass to GetProcAddress. The way you export currently, it'd be a decorated name.
Sign in to comment