I’d like to ask for your help. I am new in this and I can’t get to make MOVE ctor work.
My move CTOR is not getting hit for some reason I dont understand.
Please help me.
Thank you so much in advance.
#include<iostream>
class DynamicInt
{
public:
DynamicInt()
: intPtr{ new int {0} }
{
}
DynamicInt(int value)
: intPtr{ new int { value } }
{
std::cout << "one parameter ctor" << std::endl;
}
~DynamicInt()
{
delete intPtr;
std::cout << "Destructed." << std::endl;
}
// COPY CTOR
DynamicInt(const DynamicInt& other)
: intPtr{ new int { other.GetValue() } }
{
std::cout << "copy CTORn";
}
// MOVE CTOR -->>>> NOT GETTING HIT !!!!! :(
DynamicInt(DynamicInt&& other) noexcept
: intPtr { other.intPtr }
{
std::cout << "move con called. n";
other.intPtr = nullptr;
}
private:
int* intPtr;
};
DynamicInt ConstructorDynamicInt(int val)
{
DynamicInt dInt{ val };
return dInt;
}
int main()
{
DynamicInt intCOne = ConstructorDynamicInt(20);// should trigger either MOVE or COPY constructor
std::cout << intCOne.GetValue() << std::endl;
}
I’ve already tried searching in google.Still wouldn’t work.
From someone else’s setup (saw on video), the same exact code works.
New contributor
Xmaki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2