Move the same unique_ptr into function in a loop
up vote
1
down vote
favorite
Whats the best way to redesign the following error prone code:
void ClassA::methodA(std::unique_ptr<ClassB::ISomeInterface> obj){
for (int i = 0; i < 10; i++) {
methodB(std::move(obj)); // the obj pointer is undefined on second iteration here after the move
}
}
void ClassA::methodB(std::unique_ptr<ClassB::ISomeInterface> obj){
..........
}
The goal is to pass the same unique_ptr to function multiple times.
c++ shared-ptr unique-ptr
add a comment |
up vote
1
down vote
favorite
Whats the best way to redesign the following error prone code:
void ClassA::methodA(std::unique_ptr<ClassB::ISomeInterface> obj){
for (int i = 0; i < 10; i++) {
methodB(std::move(obj)); // the obj pointer is undefined on second iteration here after the move
}
}
void ClassA::methodB(std::unique_ptr<ClassB::ISomeInterface> obj){
..........
}
The goal is to pass the same unique_ptr to function multiple times.
c++ shared-ptr unique-ptr
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Whats the best way to redesign the following error prone code:
void ClassA::methodA(std::unique_ptr<ClassB::ISomeInterface> obj){
for (int i = 0; i < 10; i++) {
methodB(std::move(obj)); // the obj pointer is undefined on second iteration here after the move
}
}
void ClassA::methodB(std::unique_ptr<ClassB::ISomeInterface> obj){
..........
}
The goal is to pass the same unique_ptr to function multiple times.
c++ shared-ptr unique-ptr
Whats the best way to redesign the following error prone code:
void ClassA::methodA(std::unique_ptr<ClassB::ISomeInterface> obj){
for (int i = 0; i < 10; i++) {
methodB(std::move(obj)); // the obj pointer is undefined on second iteration here after the move
}
}
void ClassA::methodB(std::unique_ptr<ClassB::ISomeInterface> obj){
..........
}
The goal is to pass the same unique_ptr to function multiple times.
c++ shared-ptr unique-ptr
c++ shared-ptr unique-ptr
asked Nov 10 at 19:04
Steve Roy
44
44
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
If you don't want to transfer ownership just pass the raw pointer or a reference. If the functions are going to store the pointer a shared_ptr
would be more appropriate:
void ClassA::methodA(std::unique_ptr<ClassB::ISomeInterface> obj){
for (int i = 0; i < 10; i++) {
methodB(*obj);
}
}
void ClassA::methodB(ClassB::ISomeInterface& obj){
..........
}
add a comment |
up vote
1
down vote
Pass it by (optionally const
) reference to methodB.
So instead of having
void ClassA::methodB(std::unique_ptr<ClassB::ISomeInterface> obj);
you can have either of the following
void ClassA::methodB(const ClassB::ISomeInterface& obj);
or
void ClassA::methodB(ClassB::ISomeInterface& obj);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
If you don't want to transfer ownership just pass the raw pointer or a reference. If the functions are going to store the pointer a shared_ptr
would be more appropriate:
void ClassA::methodA(std::unique_ptr<ClassB::ISomeInterface> obj){
for (int i = 0; i < 10; i++) {
methodB(*obj);
}
}
void ClassA::methodB(ClassB::ISomeInterface& obj){
..........
}
add a comment |
up vote
2
down vote
accepted
If you don't want to transfer ownership just pass the raw pointer or a reference. If the functions are going to store the pointer a shared_ptr
would be more appropriate:
void ClassA::methodA(std::unique_ptr<ClassB::ISomeInterface> obj){
for (int i = 0; i < 10; i++) {
methodB(*obj);
}
}
void ClassA::methodB(ClassB::ISomeInterface& obj){
..........
}
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
If you don't want to transfer ownership just pass the raw pointer or a reference. If the functions are going to store the pointer a shared_ptr
would be more appropriate:
void ClassA::methodA(std::unique_ptr<ClassB::ISomeInterface> obj){
for (int i = 0; i < 10; i++) {
methodB(*obj);
}
}
void ClassA::methodB(ClassB::ISomeInterface& obj){
..........
}
If you don't want to transfer ownership just pass the raw pointer or a reference. If the functions are going to store the pointer a shared_ptr
would be more appropriate:
void ClassA::methodA(std::unique_ptr<ClassB::ISomeInterface> obj){
for (int i = 0; i < 10; i++) {
methodB(*obj);
}
}
void ClassA::methodB(ClassB::ISomeInterface& obj){
..........
}
answered Nov 10 at 19:08
Alan Birtles
7,360733
7,360733
add a comment |
add a comment |
up vote
1
down vote
Pass it by (optionally const
) reference to methodB.
So instead of having
void ClassA::methodB(std::unique_ptr<ClassB::ISomeInterface> obj);
you can have either of the following
void ClassA::methodB(const ClassB::ISomeInterface& obj);
or
void ClassA::methodB(ClassB::ISomeInterface& obj);
add a comment |
up vote
1
down vote
Pass it by (optionally const
) reference to methodB.
So instead of having
void ClassA::methodB(std::unique_ptr<ClassB::ISomeInterface> obj);
you can have either of the following
void ClassA::methodB(const ClassB::ISomeInterface& obj);
or
void ClassA::methodB(ClassB::ISomeInterface& obj);
add a comment |
up vote
1
down vote
up vote
1
down vote
Pass it by (optionally const
) reference to methodB.
So instead of having
void ClassA::methodB(std::unique_ptr<ClassB::ISomeInterface> obj);
you can have either of the following
void ClassA::methodB(const ClassB::ISomeInterface& obj);
or
void ClassA::methodB(ClassB::ISomeInterface& obj);
Pass it by (optionally const
) reference to methodB.
So instead of having
void ClassA::methodB(std::unique_ptr<ClassB::ISomeInterface> obj);
you can have either of the following
void ClassA::methodB(const ClassB::ISomeInterface& obj);
or
void ClassA::methodB(ClassB::ISomeInterface& obj);
answered Nov 10 at 19:08
druckermanly
1,252721
1,252721
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242433%2fmove-the-same-unique-ptr-into-function-in-a-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown