Perhaps I was trying to be too generic. (Original question below) Concretely, I have some dependency Dep of a class Foo. I also have a class MockDep and am defining a class TestFoo. Here is its constructor I tried to write:
TestFoo(unique_ptr<MockDep> dep) : Foo(std::move(dep)), mock_dep_(dep.get()) {}
And Foo's constructor looks like:
Foo(unique_ptr<Dep> dep) : dep_(dep) {}
mock_dep_ is delcared in TestFoo as MockDep* mock_dep_, and dep_ is declared in Foo as unique_ptr<Dep> dep_. How can I get mock_dep_ to contain dep_'s address? (as the above doesn't work since std::move(dep) nulls out dep.)
Original post:
I have an object of type Foo that I'm to pass to a different object of type OtherObject which claims ownership of it, but as a pointer to its base class. However, I want to grab a pointer to the child object that I can use to reference it. I wrote something like:
Foo(std::unique_ptr<Child> thing) :
OtherObject(std::move(thing)), child_(thing.get()) {}
OtherObject(std::unique_ptr<Base> thing, ...) { ... }
However, this doesn't seem to work, as the std::move(thing) seems to null out the pointer that returns from thing.get() later.
I can change Foo's parameter to be of type Child* instead of unique_ptr<Child>, but I'd prefer to be able to do the latter as it explicitly documents the ownership semantics.
What's the most appropriate (or failing that, unobtrusive) way of resolving this?
edit: Foo and OtherObject are both meant to be classes whose constructors I'm defining.