U E D R , A S I H C RSS

More EffectiveC++/Operator

1. Operator

1.1. Item 5: Be wary of user-defined conversion functions.

  • Item 5: ‚ฌšฉž  •˜ ˜•€™˜(conversion) •ˆ˜— ฃผ˜•˜ผ!

  • C++Š” ƒ€ž…๊ฐ„˜ •”‹œ  type casting„ —ˆšฉ•œ‹ค. ๊ฑ C˜ œ ‚ฐธฐ ˜ˆฅผ “คž char๊ณผ int —„œ short๊ณผ double “ค •„Ÿฐ ฌธ œ—† ฐ”€Œ– ง„‹ค. ๊ทธŸฐฐ C++Š” ๊ฒƒ ‹ค •œˆ˜ ”– „œ type casting‹œ— žฃŒฅผ žƒ– ฒ„ฆฌ๊ฒŒ ˜Š” int—„œ short๊ณผ dougle—„œ char˜ €™˜๊นŒ€ —ˆšฉ•œ‹ค.

ผ‹จ Ÿฐ ๊ธฐณธ €™˜— Œ€•„œ ๊ฐœฐœžŠ” –ฐŒ ๊€—ฌ • ˆ˜ —†‹ค. •˜€งŒ, C++—„œ class˜ ˜•€™˜€ ๊ฐœฐœž๊ฐ€ ˜•€™˜— ๊€•˜—ฌ ๊€—ฌ• ˆ˜ žˆ‹ค. €™˜— ๊€•˜—ฌ …ผ•œ‹ค.

  • C++—„œŠ” ฌ๊ฒŒ ‘๊ฐ€€ ฐฉ‹˜ •ˆ˜กœ ˜•€™˜„ ŒŒผŸฌ—๊ฒŒ ˆ˜–‰ ‹œ‚ค‚จ‹ค:
    single-argument constructors ™€ implicit type conversion operators  ๊ทธ๊ฒƒ‹ค.
  • single-argument constructors € ธžฅผ •˜‚˜˜ ธžงŒœผกœ „ธŒ… ˆ˜ žˆŠ” ƒ„ฑž‹ค. —ฌ๊ธฐ ‘๊ฐ€€˜ ˜ˆฅผ ž

~cpp 
class Name {
public:
    Name( const string& s);
    ...
};
class Rational {
public:
    Rational( int numerator = 0, int denominator = 1);
    ...
};

  • implicit type conversion operator € ž˜Šคกœ •˜—ฌ๊ธˆ •‹น ƒ€ž…œผกœ return „ ›• •Œ •”‹œ ธ €™”ฅผ €›•˜๊ธฐ œ„•œ operator‹ค. •„ž˜Š” doubleกœ˜ ˜•€™˜„ œ„•œ ๊ฒƒ‹ค.

~cpp 
class Rational{
public:
    ...
    operator double() const;
};

Rational r(1,2);
dougle d = 0.5 * r;
ฐธ ๊œฐฎ€ ฐฉ•‹ค. •˜€งŒ  ฐฉ•€ ๊ฐœฐœž๊ฐ€ ˜„•˜€ •Š€ ˜•€™˜งˆ ธ ‹œ‚คŠ” ๊ฒƒ•Œฌธ— ฌธ œ๊ฐ€ ฐœƒ•œ‹ค. ‹คŒ„ ž
~cpp 
Rational (1,2);
cout << r;      // should print "1/2"
operator<<Š” ฒ˜Œ Raional ผŠ” ˜•— Œ€•œ ž‹ ˜ Œ€‘„ ฐพ€งŒ —†๊ณ , ฒˆ—Š” r„ operator<<๊ฐ€ ฒ˜ฆฌ• ˆ˜ žˆŠ” ˜•œผกœ €™˜‹œ‚ค คŠ” ž‘—…„ •œ‹ค. ๊ทธŸฌŠ” ™€ค‘— r€ doubleกœ •”‹œ  €™˜ ฃจ– €๊ณ  ๊ฒฐ๊ณผ double ˜•œผกœ ถœ ฅ œ‹ค.

ญ Ÿฐ •”‹œ  ˜•€™˜„ ง‰„ ค, ˜• „™˜ ‹œ‚ค๊ณ  •˜Š” •”‹œ  ‚ฌšฉ„ •˜€ •Š๊ณ , ‹คฅธ •ˆ˜กœ …‹œ œผกœ • ค„ˆ˜ žˆ‹ค.
~cpp 

class Raional{
public:
    ...
    double asDouble() const;
};

Rational r(1,2);
cout << r;                // error!
cout << r.asDouble();     // doubleกœ˜  „™˜˜ ˜„๊ฐ€ ™•‹คžˆ  „• ง„‹ค.

Ÿฐ ˜ˆกœ C++ std library— žˆŠ” string char*กœ •”‹œ  ˜•€™˜ —†๊ณ  c_str˜ …‹œ  ˜•€™˜ ‹œ‚จ‹ค.

  • single-argument constructor Š” ” – คš ฌธ œฅผ  œ๊ณต•œ‹ค. ๊ฒŒ‹ค๊ฐ€ ฌธ œ“ค€ •”‹œ  ˜•€™˜ ‹ค ” งŽ€ €ถ„„ ฐจ€•˜Š” •”‹œ  ˜•€™˜—„œ ฌธ œ๊ฐ€ ฐœƒœ‹ค.

~cpp 
template<class T>
class Array{
public:
    Array ( int lowBound, int highBound );
    Array ( int size )

    T& operator[] (int index)
    ...
};

ฒซฒˆงธ ƒ„ฑžŠ” ฐฐ—˜ lowBound~highBound ‚ฌกœ˜ ฌ๊ธฐ  œ•œž๊ณ , ‘ฒˆงธ ƒ„ฑžŠ” •‹น ฌ๊ธฐกœ ฐฐ— ๊ณต๊ฐ„ ƒ„ฑธฐ,  ‘ฒˆงธ˜ ƒ„ฑž๊ฐ€ ˜•€™˜„ ๊ฐ€Šฅ•˜๊ฒŒ งŒ“ค–„œ •œ•œ ‚ฝงˆ— „ธ๊ณ„— ‹น‹ „ ˆŒ€•œ‹ค. (‹ค œกœ Ÿฐ ˜ฏธกœ จžˆ‹ค. --ƒฏผ)

˜ˆฅผ “ค–„œ ‹คŒ„ ž
~cpp 
bool operator==( const Array< int >& lhs, const Array<int>& rhs);

Array<int> a(10);
Array<int> b(10);
...
for ( int i = 0; i<10; ++i)
    if( a == b[i] ) {           // —‰Šค! Ÿฐ "a"Š” "a[i]" จ••  ฝ”“œ˜€‹ค!. (๊ฐœฐœž˜ ‹คˆ˜ ˜ฏธ, •œฏธ –‘๊ตญ—„œ ๊ฐ™€ ฐœŒ˜ oops! --;;  --ƒฏผ)
        a[i]™€ b[i]๊ฐ€ ๊ฐ™„•Œ  ฝ”“œฅผ ˆ˜–‰•œ‹ค.;
    }
    else {
        œ„˜ กฐ๊ฑ„ งŒกฑ•˜€ •˜  ฝ”“œฅผ ˆ˜–‰•œ‹ค.;
    }
7ค„ if ( a == bi ) €ถ„˜ ฝ”“œ—„œ ”„กœ๊ทธž˜จธŠ” ž‹ ˜ ˜„™€Š” ‹คฅธ ฝ”“œฅผ ž‘„ฑ–ˆ‹ค. Ÿฐ ฌธ• ž˜€ ‹น—ฐžˆ! ŒŒผŸฌ๊ฐ€ •Œ คค˜•ผ ๊ฐœฐœž˜ ‹œ๊ฐ„„ •„‚„ˆ˜ žˆœผฆฌ, •˜€งŒ Ÿฐ ˜ˆ œ๊ฐ€ ๊ผญ ๊ทธ ‡€งŒ€ •Š‹ค.  ฝ”“œŠ” ŒŒผŸฌ ž…žฅ—„œ ˜ณ€ ฝ”“œ๊ฐ€  ˆ˜ žˆŠ” ๊ฒƒ‹ค. ฐ”กœ Array class—„œ  •˜ •˜๊ณ  žˆŠ” single-argument constructor — ˜•˜—ฌ ŒŒผ‹œ Ÿฐ ฝ”“œกœ˜ €™˜˜ ๊ฐ€Šฅ„ฑ žˆ‹ค.

~cpp 
for ( int i = 0; i < 10; ++i)
    if ( a == static_cast< Array<int> >(b[i]) )...
bi Š” int˜•„ ฐ˜™˜•˜๊ธฐ •Œฌธ—  ‡๊ฒŒ ฆ‰„—„œ งžถค ƒ„ฑžกœ type casting(˜•€™˜)„ ŒŒผŸฌ๊ฐ€ •”‹œ œผกœ •ค„ˆ˜ žˆ‹ค.  œ ‚ฌƒœ˜ ‹ฌ๊ฐ„ฑ„ •Œ๊ฒ Š”๊ฐ€?

  • explicit
Ÿฐ • งค•œ ƒ™„ ”ผ• ˆ˜ žˆŠ” ๊ฐ€žฅ šจ๊ณผ ธ ฐฉ•€ C++—„œ “ฑžฅ•œ ƒˆกœš ‚ค›Œ“œธ explicit ˜ ‚ฌšฉ‹ค.  ‚ค›Œ“œ๊ฐ€ ™€ ƒ„ฑžกœ˜ ˜•€™˜—„œŠ” ฐ˜“œ‹œ …‹œ ธ „ –ธ žˆ–•ผ ๊ฐ€Šฅ•˜‹ค. ฆ‰ œ„˜ ฝ”“œฅผ ‹ค‹œ ž‘„ฑ•˜—ฌ explicit˜ ‚ฌšฉ„ •Œ•„๊ณ  ฌธ•ƒ œ ˜ ‚ฌ•„ •Œ•„ ž

~cpp 
template<class T>
class Array{
public:
    ...
    expicit Array ( int size )   
    ...
};

Array<int> a (10); 
Array<int> b (10);       // ‘๊ฐœ ‘ ƒ„ฑ ๊ฐ€Šฅ•˜‹ค

if ( a == Array<int>(b[p])) ...                  //  ฝ”“œ —ญ‹œ ˜ฌฐ”‹ค. 
                                                 // •˜€งŒ ‹คฅธ ๊ฐœฐœž๊ฐ€ •„— ‚ฌšฉž˜ ˜„๊ฐ€ •ฝ๊ฐ„ ˜ฌธ ๊ฐ„‹ค.
if ( a == static_cast< Array<int> >(b[p])) ...   // งž‹ค. œ„™€ งˆฐฌ๊ฐ€€กœ ‹คฅธ ‚ฌžŒ •€˜ฆฐ‹ค
if ( a == (Array<int>) b[i] )                    // C-style ˜ ˜•€™˜ธฐ —ญ‹œ‚˜ ‹คฅธ‚ฌžŒ —ฐ›Š”‹ค.
 ‡๊ฒŒ explicitฅผ ‚ฌšฉ•˜ …‹œ œผกœงŒ ˜•€™˜ ๊ฐ€Šฅ•˜‹ค. •˜€งŒ ˜ •˜‚˜ ฌธ•ƒ œ ˜ ••• ‚ฌ•€
~cpp 
static_cast< Array<int> >(b[p])
 ๊ตฌถ„—„œ > >  ‘๊ฐœฅผ ™—ฌ“ฐ >> operatorกœ •„•˜‹ˆ œ ˜•ผ

—ฌ๊ธฐ— •œ๊ฐ€€ ” ƒ๊ฐ• ž.

งŒ•‹น‹ ˜ ŒŒผŸฌ๊ฐ€ —„ฒญ ‚ก€ ๊ฑฐ๊ฑฐ‚˜  œž‘ž๊ฐ€ ๊นœนก•„œ, ˜น€ explicitฅผ ฃฝ–„ „ฃ๊ธฐ ‹ซ‹ค๊ณ  • •Œ—Š”?

—ฌ๊ธฐ— ‚˜˜จ Array ˜ˆ œฅผ •œฒˆ ๊ณ ณ„œ งˆน˜ explicit ฅผ “๊ฒƒฒ˜Ÿผ •ž
~cpp 
template<class T>
class Array{
public:
    class ArraySize{
    public:
        ArraySize(int numElements):theSize(numElements){}
        int size() const { return theSize; }
    private:
        int theSize;
    }
    Array( int lowBound, int highBound);
    Array( ArraySize size );                      // ƒˆกœš „ –ธ!
    ...
}

 ‡๊ฒŒ Array •ˆ— ArraySize ฅผ „ –ธ•˜๊ณ  publicœผกœ ถˆ–„œ  ‡๊ฒŒ ƒ„ฑ•˜
~cpp 
Array<int> a(10);

ŒŒผŸฌ ‹จ—„œ aƒ„ฑžธ Array( ArraySize size) —„œ ArraySize กœ˜ single-argument constructorฅผ ˜ธถœ•˜๊ธฐ •Œฌธ— „ –ธ ๊ฐ€Šฅ•˜€งŒ
~cpp 
bool operator==( const Array< int >& lhs, const Array<int>& rhs);

Array<int> a(10);
Array<int> b(10);
...
for ( int i = 0; i<10; ++i)
    if( a == b[i] ) 
Ÿฐ ๊ฒฝšฐ— operator==˜ ˜คฅธ— žˆŠ” ธžŠ” int๊ฐ€ single-argument constructor— ๊ฑฐน ˆ˜ —†๊ธฐ •Œฌธ— —Ÿฌฅผ ฐท– ‚ธ‹ค.

*›„๊ธฐ:ฒˆ๊€ „ˆ ๊ธธ‹ค. ‹คฅธ๊ฑฐ— ‘ฐฐ— •‹น•˜Š”๊ฑฐ ๊ฐ™€ฐ ‹คŒ€„ฐŠ” ”ฒญ ”ผšฐ€ ง๊ณ  ••€ --ƒฏผ

1.2. Item 6: Distinguish between prefix and postfix forms of increment and decrement operators.

  • Item 6: prefix™€ postfixกœ˜ ฆ๊ฐ —ฐ‚ฐž ๊ตฌถ„•˜ผ!

šฐฆฌŠ” ++™€ --—ฐ‚ฐž(•˜ ๊ฐ€นญ ๊ฐ€๊ฐ —ฐ‚ฐž)ฅผ ฆ๊ฒจ “‹ค.  —ฐ‚ฐž —ญ‹œ ž˜Šค—„œ  •˜•„œ ‚ฌšฉ• ˆ˜ žˆ‹ค.
•˜€งŒ  ๊ฐ€๊ฐ —ฐ‚ฐžŠ” ‘๊ฐ€€กœ ‚˜‰œ‹คŠ” ‚ฌ‹ค„ ƒ๊ฐ•˜ ๊ฐ‘ž๊ธฐ ‚œ๊ฐ• ง„‹ค. „คงˆ „ค๊ณ„ž๊ฐ€ ๊ทธŸฐ ‹จ!ˆœ!•œ! ฌธ œฅผ ๊ฐ„๊ณผ• ฆฌ —†‹ค.

~cpp 
class UPInt{  //•œ  •ˆ˜˜•
public:
    UPInt& opertrator++(int);         // prefix++
    const UPInt operator++(int);      // postfix++

    UPInt& opertrator--(int);         // prefix--
    const UPInt operator--(int);      // postfix--

    UPInt& operator+=(int);           // a += operator Š” UPInt™€ ints ถ”๊ฐ€ œ„•
...
};
UPInt i;

++i;        // i.operator++();
i++;        // i.operator++(0);

--i;        // i.operator--();
i--;        // i.operator--(0);

๊ณ  žˆž  •ง •„””–œฐฎ€“ฏ ๊ทธŸผ ๊ตฌ ธ ๊ตฌ˜„€— ๊€•„œ ๊ฐ„‹จžˆ …ผ•œ‹ค.
~cpp 
// prefix ฆ๊ฐ€ —ฐ‚ฐž €ถ„๊ณผ fetch
UPInt& UPInt::operator++()
{
    *this += 1;       // ฆ๊ฐ€
    return *this      // fetch!
}

// postfix ฆ๊ฐ€ —ฐ‚ฐž €ถ„๊ณผ fetch
const UPInt& UPInt::operator++(int)
{
    UPInt oldValue = *this;             // fetch
    ++(*this);                          // prefix ฆ๊ฐ€ ‹œ‚จ‹ค.
    return oldValue;
}

*ž‘„ฑž ‚ฌ„ค: •„ ‚˜Š”  •ง Ÿฐ ฆฌ„ •๊ฐ€ •ˆ๊ฐ„‹ค. ฐธกฐกœ „˜๊ฒจ ฒ„ฆฌ Œ€ ŒŒผŸฌŠ” –А ‹œ —„œ oldValue˜ ŒŒ๊ฅผ •˜ƒ” ง‹ค. C++ reference countingœผกœ ž› ๊€ฆฌฅผ ”ฐกœ •ฃผŠ” ๊ฒƒ„ •„‹ŒŒ€ ง‹ค. 1•™…„•Œ €„ฐ˜ ๊ณ ฏผ‹จ ง‹ค. € …พŒ•œ „ค…„ ˆ„๊ฐ€ •คฌœผ..

•”Šผ  € œ„™€ ๊ฐ™ •˜ •๊ฐ€ ๊ฐˆ๊ฒƒ‹ค. •˜€งŒ Ÿ ๊ฒฝšฐ š” ง“๊ฑฐฆฌฅผ •œ‹ค.

~cpp 
    UPint i;
    i++++;                 
 ˜ฏธŠ”
~cpp 
    i.operator++(0).operator++(0);
๊ณผ ๊ฐ™‹ค. ‹น—ฐžˆ •ˆ˜๊ฒ €?

  • ž‘„ฑž ‚ฌ„ค:ณธฌธ—„œŠ” ๊ทธ ’ค€„ฐŠ” •„˜ˆ Ÿฐ๊ฑธ “ฐ€ งžŠ” •„š”„ฑ˜ –ธ๊ธ‰‹ˆ‹ค. ฐจ›„ ถ”๊ฐ€˜ •„š”„ฑ žˆ„•Œ ถ”๊ฐ€•‹ˆ‹ค.
    ๊ทธƒฅ i++ ‘ฒˆ “ฐ๊ณ  ง€..

1.3. Item 7: Never overload &&, ||, or ,.

  • Item 7:  ˆŒ€กœ! &&, ||, ','  —ฐ‚ฐž“ค„ overload •˜€ ง•„ผ


~cpp 
    char *p;
    ...
    if ((p != 0) && (strlen(p) > 10) ) ...
œ„˜ ฝ”“œ—„œŠ” strlen() •ˆ˜‚€—„œ p— ๊€ จ•œ null pointer ๊ฒ€‚ฌ๊ฐ€ •„š”•˜€ •Š‹ค. ™œƒ•˜ && —„œŠ” •ž˜ กฐ๊ฑ € • ฆ‰, ( false && anything ) ˜ ๊ฒฝšฐ—Š” ’ค˜ กฐ๊ฑ(anything)€ ˆ˜–‰กฐฐจ •ˆ•˜๊ธฐ •Œฌธ‹ค. operator ||˜ ๊ฒฝšฐ„ Šน • กฐ๊ฑ—„œ,(true || anything) ’ค— ฝ”“œฅผ ˆ˜–‰•˜€ •Š€‹คŠ” ๊ฒƒ€ น„Šท•˜‹ค.
~cpp 
    int rangeCheck( int index)
    {
        if ((index < lowerBound) || (index > upperBound)) ...
        ...
    }

ˆ˜งŽ€ ๊ฐœฐœž“ค Ÿฐ ‹จˆœ•œ ›ฆฌฅผ ”„กœ๊ทธžจ ƒ—„œ˜ งง€ ง„–‰(short-circuit)„ ถ”๊ตฌ•˜Š”ฐ ‚ฌšฉ•˜˜€‹ค. ๊ทธ ‡‹ค C++—„œ˜ ๊ฐ“ค—๊ฒŒ operator ||, && ฅผ overload ‹œ‚ค งง€ ง„–‰„ ถ”๊ตฌ•˜Š”ฐ „›€ ˜€ •Š„๊นŒ? ๊ทธŸฐฐ •˜€ งผ‹ˆ ™œผ๊นŒ?

ผ‹จ operator &&, ||Š”  ‡๊ฒŒ ‘๊ฐ€€˜ ๊ฒฝšฐกœ ‘˜ˆ˜ žˆ‹ค.
~cpp 
    if (expression1 && expression2) ...
 ฌธžฅ˜ •„„ ŒŒผŸฌŠ”  ‡๊ฒŒ ฐ›•„ “คธ‹ค.
~cpp 
    1. if (expression1.operator&&(expression2))...   // when operator && is member function.
    2. if (operator&&(expression1 , expression2))... // global function

ž  ‘๊ฒฝšฐ ‘ฅผ ƒ๊ฐ• 1,2 –‘‹ค expression1, expression2 ˜ ๊ฒฐ๊ณผ ๊ฐ’ •„š”•œ ƒ™‹ค. ฆ‰, operator && ‚˜ operator || ˜ ๊ฒฝšฐ –‘ classธž“ , ––ค ˜•ƒœ“  ฐ˜“œ‹œ ๊ฒฐ๊ณผ ๊ฐ’ •„š”•˜‹ค. œ„—„ –ธ๊ธ‰–ˆ€งŒ, ฏธ งŽ€ ๊ฐœฐœž“ค &&™€ ||˜ Šน„ฑ„ ž˜ •Œ๊ณ  ‚ฌšฉ•˜๊ณ  žˆœผฉฐ, operator &&, ||˜ overloadŠ” ๊ตฌ™˜€ ง•„••  ฝ”“œ๊ฐ€ ๊ตฌ™˜Š” ˜„•˜€ •Š€ ˜คฅ˜๊ฐ€ ฐœƒ †Œ€๊ฐ€ žˆ‹ค.

"comma operator" —ญ‹œ งˆฐฌ๊ฐ€€‹ค. comma operator๊ฐ€ Œ€ ญƒ๊ณ ?

comma operatorŠ” ‘œ˜„(form expression)— ‚ฌšฉœ‹ค. •„ž˜ฅผ ž
~cpp 
void reverse(char s[])
{
    for (int i = 0, j = strlen(s)-1;
        i < j;
        ++i, --j)                   // …€„“ค ฐ”กœ comma operator“ค‹ค. ๊ธฐ๊ฐ€ ง‰˜€..
    {
        int c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}
๊ฒฐก € overloadกœ –ธ–ƒ˜ …€„“ค ›ž˜˜ Šฅ ฅ ฐœœ˜•˜๊ธฐ๊ฐ€ ž˜“ค‹ค. Ÿฐ œ„—˜€ ๊ฐˆ˜•  •„š” —†€ •Š€๊ฐ€? ฐธ๊ณ กœ ‹คŒ„ •Œž

overload• ˆ˜ —†Š” operator
~cpp 
    .                .*                ::                ?:
    new              delete            sizeof            typeid
    static_cast      dynamic_cast      const_cast        reinterpret_cast
overlod • ˆ˜ žˆŠ” operator
~cpp 
    operator new     operator delete
    operator new[]   operator delete[]
    +    -    *    /    %    ^    &    |    ~
    !    =    >    <    +=   -=   *=   /=   &=
    ^=   &=   |=   <<   >>   >>=  <<=  ==   !=
    <=   >=   &&   ||   ++   --   ,    ->*  ->
    ()   []

1.4. Item 8: Understand the differend meanings of new and delete

  • Item 8: new™€ delete๊ฐ€ “ฐž„— ”ฐฅธ ˜ฏธ“ค˜ ฐจฅผ ••˜ผ.

†ต C++—„œ šฉ–“ค„  •™•žˆ • •  ๊ฒฝšฐ๊ฐ€ žˆ‹ค. ฐ”กœ newoperator™€ operator new๊ฐ€ ๊ทธ Œ€‘œ ธ ˜ˆ๊ฐ€  ˆ˜žˆ„ ๊ฒƒ‹ค. ‹คŒ˜ ฝ”“œฅผ ž
~cpp 
    string *ps = new string("Memory Management");

 ฝ”“œŠ” new operatorฅผ ‚ฌšฉ•œ ๊ฒƒ‹ค. new operatorŠ” sizeof ฒ˜Ÿผ –ธ– ƒ— ฌ•˜– žˆœผฉฐ, ๊ฐœฐœž๊ฐ€ ” ƒ ๊ทธ ˜ฏธ˜ €๊ฒฝ ถˆ๊ฐ€Šฅ•˜‹ค. ๊ฑ ‘๊ฐ€€˜ —ญ• „ •˜Š”ฐ, ฒซงธกœ •‹น ๊ฐ๊ฐ€ “ค–๊ฐˆ งŒ•œ ฉ”ฆฌฅผ • ‹น•˜Š” ๊ฒƒ๊ณ , ‘˜งธกœ •‹น ๊ฐ˜ ƒ„ฑžฅผ ถˆŸฌฃผŠ” —ญ• ‹ค. new operatorŠ” •ƒ  ‘๊ฐ€€˜ ˜ฏธผ ž‘™•˜ฉฐ •ž—„œ –ธ๊ธ‰•œ“ฏ €๊ฒฝ€ ถˆ๊ฐ€Šฅ•˜‹ค.

†ต operator newŠ”  ‡๊ฒŒ „ –ธ˜– žˆ‹ค.
~cpp 
    void * operator new(size_t size);
๊ฑ ๊ณผ๊ฑฐ C—„œ˜ mallocฒ˜Ÿผ ˆ๊ธฐ™” ˜€ •Š€ sizeงŒผ˜ ฉ”ฆฌฅผ • ‹น•„œ ๊ทธ๊ฑธ ๊ฐ€ฆฌ‚คŠ” void˜•˜ pointerฅผ Œ คฃผŠ” ๊ฒƒผ๊ณ  ˜ˆธก• ˆ˜ žˆ๊ฒ ‹ค.(งž‹ค) ๊ฐœฐœžŠ” operator newฅผ overload• ˆ˜ žˆ€งŒ ฒซฒˆงธ ธžŠ” •ƒ size_t๊ฐ€ ˜–••œ‹ค.

•„งˆ —ฌŸฌถ„€ operator newฅผ ง ‘ €Š”๊ฑธ ๊ฒฐฝ” ›•˜€ •Š๊ฒ €งŒ(ƒ„ฑž, ŒŒ๊žฅผ ƒ๊ฐ• ง€), จจน„ˆ˜ žˆŠ”ฐ •œฒˆ •ž.
~cpp 
    void *rawMemory = operator new(sizeof(string));

string ๊ฐ๊ฐ€ •„š”กœ •˜Š” ฉ”ฆฌ งŒผ • ‹นœ‹ค. •˜€งŒ œ„˜ ˜ฏธฒ˜Ÿผ malloc, operator newŠ” ƒ„ฑžฅผ ˜ธถœ•˜€Š” •–Š”‹ค. ƒ„ฑž˜ ˜ธถœ€ new operator ‹ค.

new operatorฅผ
~cpp 
    string *ps = new string("Memory Management");
‹คŒ๊ณผ ๊ฐ™€ ฝ”“œŠ” ŒŒผŸฌ ‹จ—„œ  ‡๊ฒŒ ๊ตœ‹ค๊ณ  ณผˆ˜ žˆ‹ค.
~cpp 
    void *memory = operator new(sizeof(string));

    call string::string("Memory Management") on *memory;

    string *ps = static_cast<string*>(memory);

•‹น ฝ”“œ ‘ฒˆงธ €ถ„˜ ƒ„ฑž ˜ธถœ„ ˆˆ—ฌ๊ฒจ ผ.

  • Placement new
‹น‹ € ƒ„ฑžฅผ ง ‘ ˜ธถœ•˜๊ธฐฅผ ›• •Œ๊ฐ€ žˆ„ ๊ฒƒ‹ค. •˜€งŒ ƒ„ฑžŠ” ๊ฐ(object)ฅผ ˆ๊ธฐ™” ‹œ‚ค๊ณ , ๊ฐ œ(object)Š” ˜คง ฒ˜Œ ฃผ–€Š” ‹จ •œฒˆ˜ ๊ฐ’œผกœ ˆ๊ธฐ™” ˜– งˆˆ˜žˆ๊ธฐ •Œฌธ— (˜ˆ-const ธˆ˜“ค ˆ๊ธฐ™”— ˆ๊ธฐ™” ฆฌŠคŠธฅผ “ฐŠ” ๊ฒฝšฐ) ƒ„ฑžฅผ ฏธ žฌ•˜๊ณ  žˆŠ” ๊ฐ— ˜ธถœ•œ‹คŠ”๊ฑ ƒ๊ฐ˜ —ฌ€๊ฐ€ —†„ ๊ฒƒ‹ค.

๊ทธ ‡€งŒ —ฌŸฌถ„ raw memoryกœ ๊ฐฅผ • ‹น•œ‹ค ˆ๊ธฐ™” ฃจ‹ •„š”•˜‹ค.

(—ฌ‹-›„›„ ‚˜„ ƒ‹จ˜ operator newฅผ Š” ˆœ๊ฐ„  ƒ๊ฐ–ˆ‹ค.)

ฐ”กœ operator new˜ Šน™” ฒ„ „ธ placement newกœ • ˆ˜ žˆ‹ค.

‹คŒ€ placement newฅผ ‚ฌšฉ•œ ˜ˆ œ‹ค.
~cpp 
    cass Widget {
        public:
            Widget(int widgetSize);
            ...
    };
    Widget* constructWidgetInBuffer(void * buffer, int widgetSize)
    {
        return new(buffer) Widget(widgetSize);
    }

•‹น •ˆ˜(construcWidgetInBuffer())Š” ฒ„ผ— งŒ“ค–ง„ Widget ๊ฐ˜ ฌธ„ฐฅผ ฐ˜™˜•˜—ฌ €‹ค.  ‡๊ฒŒ ˜ธถœ•  ๊ฒฝšฐ ๊ฐฅผ ž„˜ œ„น˜— • ‹น• ˆ˜ žˆŠ” Šฅ ฅ •Œฌธ— shared memory‚˜ memory-mapped I/O ๊ตฌ˜„— šฉ•˜‹ค constructorWidget— Š”๊ฑ ฐ”กœ
~cpp 
    return new (buffer) Widget(widgetSize);
 ฌธธฐ , •„งˆ ฒ˜Œ ๊ธฐ— ƒ†Œ•  ๊ฒƒ‹ค. ž„ธžˆ œฏ– , (buffer)— ˜•„œ •”‹œ œผกœ newŠ” operator newกœ ˜ธถœ˜– ง„‹ค. ง™—ฌ, •„ž˜˜ void*Š” ฉ”ฆฌ ƒ˜ œ„น˜ฅผ size_tŠ” ฉ”ฆฌƒ ๊ฐ๊ฐ€ ฐจ€•˜Š” ˜—ญ‹ค. ž, œ„™€ น„๊ต• ผ  operatorŠ” newฅผ overload•œ ฒ„ „‹ค.
~cpp 
    void* operator new(size_t, void *location)
    {
        return location;
    }

๊ฑฐ ๊ฐ„‹จžˆ €งŒ placement new˜  „€‹ค. operator new˜ —ญ• € •‹น ๊ฐฅผ œ„•œ ฉ”ฆฌฅผ ฐพ๊ณ (• ‹น), •‹น ฌธ„ฐ˜ ฐ˜™˜๊ณ  placement new˜ ๊ฒฝšฐ—Š” ˜ธถœž๊ฐ€ ฏธ ฉ”ฆฌฅผ ™••˜˜€๊ณ , ‹จˆœžˆ ฌธ„ฐ ฐ˜™˜งŒ •€‹ค. “  placement new๊ฐ€ ฐ˜“œ‹œ Ÿฐ pointer˜  „‹ฌ —ญ• „ •œ‹ค. ๊ทธฆฌ๊ณ  size_t ธž๊ฐ€ •„Ÿฐ ฆ„ —†–„ ฐ˜••ˆ•œ‹ค. ž„ธ•œ๊ฑ Item 6„ •๊ฐ€ ๊ฐˆ๊ฒƒ‹ค.

Ÿฐ placement newŠ” C++ ‘œ€ ผธŒŸฌฆฌ˜ •œ €ถ„œผกœ placement newฅผ “ฐ๊ณ ž •œ‹ค #include<new> ฅผ •ฃผ œ‹ค.

  • new ๊ฒฐก !
žž new ๊ฒฐก ‹ค.

the new operator : heap ฉ”ฆฌ ™•™€ ƒ„ฑž ˜ธถœ ™‹œ—

operator new : •‹น ๊ฐ˜ ฉ”ฆฌ ™•งŒ

placement new : ฏธ ™• ฉ”ฆฌ๊ฐ€ žฌ•˜๊ณ  ๊ฐ ˆ๊ธฐ™”ฅผ ›•œ‹ค

‹ค –„ค”„๊ฒŒ  •ฆฌ–ˆ๊ณ  ฐจ›„ ถ”๊ฐ€ • ๊ฒƒ‹ค.

  • Deletion and Memory Deallocation
™  • ‹น— Œ€•˜—ฌ ฆฌ†ŒŠค ƒˆŠ”๊ฑธ ฐฉ€•  ค •‹น ™  • ‹น— ƒ‘•˜Š” ‘•(?) •„𔕠 ๊ฒƒ‹ค. ˜ˆฅผ“ค–„œ new‚˜ operator new—Š” Ÿฐ๊ฑฐ
~cpp 
    string *ps;
    ...
    delete ps;      // delete operator ฅผ ‚ฌšฉ•œ‹ค.
‹น‹  “ฐ๊ณ  žˆŠ” ŒŒผŸฌŠ” ๊ฐ ps pointฅผ ŒŒ๊•˜๊ณ  ๊ฐ๊ฐ€ ๊ฐ€€๊ณ  žˆŠ” ฉ”ฆฌฅผ • œ•œ‹ค.

ฉ”ฆฌ • œ(deallocaion)€ operator delete•ˆ˜— –‰•€Š”ฐ ผฐ˜ œผกœ  ‡๊ฒŒ „ –ธ˜– žˆ‹ค.
~cpp 
    void operator delete(void * memoryToBeDeallocated);
๊ทธŸฌ€กœ
~cpp 
    delete ps;
Š” Ÿฐ ฝ”“œ๊ฐ€ ˆ˜–‰œ‹ค๊ณ  œ‹ค.
~cpp 
    ps->~string();
    operator delete(ps);
(ž‘„ฑž ฃผ: ๊ฑธกœ new™€ delete˜ ™˜ƒ,‹ น„„ฑ ๊นจกŒ‹ค. )

๊ทธฆฌ๊ณ  ๊ฒƒ˜ ˜ฏธŠ” ‹น‹  ˆ๊ธฐ™” ˜€ •Š€ rawกœ˜ ‚ฌšฉ˜ ๊ฒฝšฐ—Š” new™€ delete operatorกœ ๊ทธƒ„˜๊ฒจ••œ‹คŠ” ˜ฏธ๊ฐ€ œ‹ค. ฆ‰ ฝ”“œ Œ€‹ — ‹คŒ˜ ˜ˆฅผ ณผˆ˜ žˆ„ ๊ฒƒ‹ค.
~cpp 
    void * buffer = operator new(50*iszeof(char));
    ...
    operator delete(buffer);
œ„˜ ฝ”“œŠ” C++ƒ—„œ malloc ๊ณผ free ฅผ ˜ธถœ ๊ฒƒ๊ณผ ™ผ•œ —ญ• „ •œ‹ค.

๊ทธ ‡‹ค, ฒˆ— ‹น‹ € placement newฅผ ‚ฌšฉ•„œ ฉ”ฆฌƒ— ๊ฐฅผ งŒ“ค—ˆ‹ค deleteฅผ ‚ฌšฉ• ˆ˜ —†„ ๊บผผŠ” ˜ˆธก„ • ˆ˜ žˆ„ ๊ฒƒ‹ค. ž ‹คŒ ฝ”“œฅผ „œ …‹œ ธ deleteฅผ –‰•˜Š” ฝ”“œ“ค„ ž
~cpp 
    void * mallocShared(size_t size);
    void freeShared(void *memory);

    void *shareMemory = mallocShared(sizeof(Widget));
    Widget *pw = constructWidgetInBuffer(shareMemory, 10);  // placement new‹ท!

    ...
    delete pw;  //  ‡๊ฒŒ •„œŠ” •ˆœ‹ค. sharedMemoryŠ” mallocShared—„œ • ‹น˜Š”ฐ –ฐŒ • ˆ˜ žˆœผฆฌš”.
                // ๊ทธ ‡‹ค ‹คŒ๊ณผ ๊ฐ™€ ฝ”“œกœ •••œ‹ค.

    pw->~Widget();  // จผ € ž„˜กœ ŒŒ๊žฅผ ˜ธถœ•œ‹ค.  ‡๊ฒŒ •˜ pw๊ณผ ๊€ จœ ž›„ ฐ˜™˜• ๊ฒƒ๊ณ .
    freeShared(pw); // ตœข… œผกœ pw๊ฐ€ • ‹นœ ฉ”ฆฌ ˜—ญ„ • œ ‹œ‚จ‹ค.

  • Arrays
ณ„‹คฅธ Šนณ„•œ € ‚ฌ• —†‹ค. —ฌƒœ ๊นŒ€˜ —ฐžฅ„ ๊ณ  ‹จ newฅผ šฉ•œ ฐฐ— • ‹น„ ‚ญ œ•  ๊ฒฝšฐ
~cpp 
    string *ps = new string[10];
    delete [] ps;
™€ ๊ฐ™ ง„ งžถ”– ฃผ–••œ‹ค. placementŠ” ‹น๊ทผ œ„˜ €‹œ ‚ฌ•— ”ฐŠ” ๊ฒƒ๊ณ  operator deleteŠ” item 6—„œ™€ ๊ฐ™ forฌธœผกœ ˆœšŒ•˜„œ €›Œ ฃผ–••œ‹ค.

งˆ€ง‰œผกœ —ฌ๊ธฐ„œ ‹ค ‹œ”ผ new™€ deleteฅผ งŒ“œŠ” žŠ” ‹น‹  กฐ • • ˆ˜ —†Š” ˜—ญ— žฌ•˜€งŒ ฉ”ฆฌ • ‹น€ ‹น‹ ˜ †•„ž˜ žˆ‹ค. new™€ deleteฅผ ตœ ™”‚˜ ˆ˜ • • •Œ ๊ผญ ๊ธฐ–ต•ผ ‹น‹   •งกœ ๊ทธ๊ฑธ • ˆ˜ —†Š”๊ฐ€— ๊€•„œ ง‹ค. ‹น‹ € ๊ทธ๊ฒƒ“ค˜ ฐฉ•(new,deleteฉ”ฆฌ • ‹น ฐฉ•)€ €๊ฒฝ• ˆ˜ žˆ‹ค. ๊ทธŸฌ‚˜ ๊ทธ“ค€ –ธ–— ˜•„œ ๊ทœ •˜–  ธ žˆŠ” ˜—ญ‹ค.


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:49
Processing time 0.0464 sec