Move unique_ptr ownership from one class to another [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to create a unique pointer in one class, class A, then pass on the ownership to another class, class B. Am I ok doing such a thing?
The code below gives me error in getC:
error: call to deleted constructor of 'std::unique_ptr<C>
What am I doing wrong?
class A {
...
void func(shared_ptr<B> Bptr) {
A_pass = make_unique<C>();
Bptr->setPass(move(A_pass));
}
unique_ptr<C> getC()
{
return A_pass;
}
unique_ptr<C> A_pass;
};
class B {
...
void setPass(unique_ptr<C> pass_ptr){
B_pass = move(pass_ptr);
}
unique_ptr<C> B_pass;
}
edit: update the question

Your question does not state where the compiler error occurs, but I would guess it’s the getC() member function:
class A {
...
unique_ptr<C> getC()
{
return A_pass;
}
unique_ptr<C> A_pass;
};
The function as written is attempting to copy A_pass, which of course is not possible for the std::unique_ptr<T> type.
You can rewrite it to explicitly move from the source (I’m not able to test this):
unique_ptr<C> A::getC() {
return A_pass.release();
// alternative: return unique_ptr<C>(std::move(A_pass));
}

Related

how can I bind a class member function with param as rvalue to boost::function? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I try to bind a class member function with param as rval to boost::function.
But it doesn't work.
my sample false code :
class Class1
{
int Foo1(int&& b)
{
return b;
}
void foo2()
{
boost::function<int(int&&)> fc(boost::bind(&Class1::Foo1, this, _1)
}
};
Use a lambda expression:
boost::function<int(int&&)> fc = [this](int&& x)
{
return Foo1(x);
};

C++ When comparing, pass class as pointer or normally? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm quite new to C++, and since I want my code to be good, I have a small question, let's say I have a function
UI::GetColor(CClass Class)
{
if (Class.m_Something)
return 0;
return 1;
}
Is it better to pass the CClass as a pointer, so it's not being copied or not? I saw a lot of code using different styles of that and I'm kind of confused which one is better and why. Thanks for answers.
The function should be written as follows:
class UI {
...
int GetColor(const CClass &c) const {
if (c.m_Something) {
return 0;
}
return 1;
}
}
The reference avoids an unnecessary copy; the const-parameter states that the parameter will not be changed; the const-function declarator states that the function will not change the this-object (i.e. the UI-instance on which it is called).

What is the size of empty derived class in c++ and java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have got some related answers like Why the size of empty class that is derived from two empty classes is 2? but not get the answer to my question clearly.
interface PI1
{
default void show()
{
System.out.println("Default PI1");
}
}
interface PI2
{
default void show()
{
System.out.println("Default PI2");
}
}
class TestClass implements PI1, PI2
{
public void show()
{
PI1.super.show();
PI2.super.show();
}
public static void main(String args[])
{
TestClass d = new TestClass();
d.show();
}
}
Does this JAVA program show multiple inheritance?
In C++ the minimum size is 1.
However, the other question is about multiple inheritance from base classes of the same type. Two objects of the same type cannot have the same address, because then they would not be different objects.
The address is an important part of the identity of an object.
So, if you have two objects of the same type, the minimum size would be 2.
None of this happens in Java, because there is no multiple inheritance.

confused about class structure in code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am reading a sample code that uses C++ and classes, I am new on C++ classes I can work with basics similar to this http://www.cplusplus.com/doc/tutorial/classes/, but I cant understand what the code below does mean or the color it is using visual studio c++
thanks
I am sorry if it is a fool question
It creates an object named some by instantiating the class some.
Then it calls the member function ToVector() on the object some and pass the result of the call to the function named function.
class is blue because it is a keyword of the C++ language.
The first some is green because it is the name of a class.
The second some is black because it is a variable.
And function and ToVector are red because the are functions.
Now this is ugly code because you "hide" the class some by reusing the same name for your variable. Also you do not need to put the word class here.
Here is a more complete and nicer version:
#include <vector>
class Some
{
public:
std::vector<int> ToVector()
{
return std::vector<int>(); //return an empty vector
}
};
int f(std::vector<int> v)
{
return 0;
}
int main(int, char**)
{
Some some; // Was "class some some"
return f(some.ToVector());
}

Returning an empty smart pointer [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a class that looks like this,
class A
{
std::shared_ptr<Type> ret;
public:
A()
{
ret=std::shared_ptr<Type>(new Type);
}
std::shared_ptr<Type> GetTypeA(){return ret;}
A (const A&a)
{
....
ret=a.ret;
}
};
class Type
{
A aa;
public:
Type(A*a):aa(*a){}
};
Somewhere in the client code, I call the method GetTypeA like this
void func(A*pA)
{
...
std::shared_ptr<Type> spT=pA->GetTypeA();
...
}
Debugging shows me that spT=empty after the call. But inside pA, ret value is NOT empty.
I notice some mistakes in your code :
A()
{
ret=std::shared_ptr<Type>(new Type);
}
"new Type" means you call default constructor for Type (Type::Type()), and you didn't write it in your sample. Try "new Type(*this)" to use your own constructor.
But to do this you need to change your Type class to:
class Type
{
A* aa; // Use a pointer
public:
Type::Type(A&a) :aa(&a){} // Use references
};
The problem is it's not resolving the "recursive aspect", depend your needs, I would use a static reference to A in the Type class...