티스토리 뷰
참고: http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html
#include <iostream>
#include <typeinfo>
#include <vector>
#include <string>
#include <utility>
using namespace std;
class MetaData
{
public:
MetaData (int size, const std::string& name)
: _name( name )
, _size( size )
{
cout << "["<< this << "] MetaData default ctor is called." << endl;
}
// copy constructor
MetaData (const MetaData& other)
: _name( other._name )
, _size( other._size )
{
cout << "["<< this << "] MetaData copy ctor is called." << endl;
}
// move constructor
MetaData (MetaData&& other)
: _name( std::move(other._name) )
, _size( other._size )
{
cout << "["<< this << "] MetaData move ctor is called." << endl;
}
~MetaData()
{
cout << "["<< this << "] metadata dtor is called." << endl;
}
std::string getName () const { return _name; }
int getSize () const { return _size; }
private:
std::string _name;
int _size;
};
template <typename t="">
class ArrayWrapper
{
public:
// default constructor produces a moderately sized array
/*
ArrayWrapper ()
: _p_vals( new T[ 64 ] )
, _size( 64 )
{
cout << "default ctor is called." << endl;
}
*/
ArrayWrapper (int n=64, const string& str="ArrayWrapper")
: _p_vals( new T[ n ] )
, _metadata( n, str )
{
cout << "<" << _metadata.getName() << ">" << endl;
cout << "["<< this << "] ArrayWrapper default ctor with size " << n << " is called. " << endl;
}
// move constructor
ArrayWrapper (ArrayWrapper<t>&& other)
: _p_vals( other._p_vals )
, _metadata( std::move(other._metadata) )
{
cout << "<" << _metadata.getName() << ">" << endl;
cout << "["<< this << "] ArrayWrapper move ctor is called." << endl;
cout << "other_p_vals: " << _p_vals << endl;
cout << "my_p_vals: " << _p_vals << endl;
other._p_vals = NULL;
}
// copy constructor
ArrayWrapper (const ArrayWrapper<t>& other)
: _p_vals( new T[ other.getSize() ] )
, _metadata( other._metadata )
{
cout << "<" << _metadata.getName() << ">" << endl;
cout << "["<< this << "] ArrayWrapper copy ctor is called." << endl;
for ( int i = 0; i < _metadata.getSize(); ++i )
{
_p_vals[ i ] = other._p_vals[ i ];
}
}
~ArrayWrapper ()
{
cout << "<" << _metadata.getName() << ">" << endl;
cout << "["<< this << "] ArrayWrapper dtor is called." << endl;
delete [] _p_vals;
}
int getSize() const
{
return _metadata.getSize();
}
private:
T *_p_vals;
MetaData _metadata;
};
/*
template <typename t="">
const ArrayWrapper<t> getArrayWarapper()
{
ArrayWrapper<t> aw(5);
return aw;
}*/
template <typename t="">
ArrayWrapper<t>&& getArrayWarapper()
{
return ArrayWrapper<t>(5, "Temp. ArrayWrapper");
}
int main( void ) {
// getArrayWarapper<int>();
ArrayWrapper<int> aw = getArrayWarapper<int>();
ArrayWrapper<int> aw2 = aw;
cout << "aw: " << aw.getSize() << endl;
cout << "aw2: " << aw2.getSize() << endl;
return 0;
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- QPrinter.A4
- 이상한 문자
- Accelerated C++
- 설치
- armadillo c++
- dll
- Visual C++
- ctypes
- QT
- TensorBoard
- destructor
- CanDrA
- Python
- structure
- 볼륨 낮춤
- QPrinter.Letter
- how to solve it
- C++
- pandas
- PyQt
- GSX 1000 pro
- TCGA
- MSVC++
- 볼륨 조절
- cython
- volume dial
- Item 9
- matrix multiplication
- GSX 1200 pro
- tensorflow
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함