constÔÚº¯ÊýÉùÃ÷ÖеÄÓ¦ÓÃ

1.ÔÚÒ»¸öº¯ÊýÉùÃ÷ÖУ¬const ¿ÉÒÔÐÞÊκ¯ÊýµÄ·µ»ØÖµ£¬»òij¸ö²ÎÊý£»¶ÔÓÚ³ÉÔ±º¯Êý£¬»¹¿ÉÒÔÐÞÊÎÊÇÕû¸öº¯Êý¡£ÓÐÈçϼ¸ÖÖÇé¿ö£¬ÒÔÏ»áÖð½¥µÄ˵Ã÷Ó÷¨£º

A& perator=(const A& a);

void fun0(const A* a );

void fun1( ) const; // fun1( ) ΪÀà³ÉÔ±º¯Êý

const A fun2( );

1£© ÐÞÊβÎÊýµÄconst£¬Èç void fun0(const A* a ); void fun1(const A& a);

µ÷Óú¯ÊýµÄʱºò£¬ÓÃÏàÓ¦µÄ±äÁ¿³õʼ»¯const³£Á¿£¬ÔòÔÚº¯ÊýÌåÖУ¬°´ÕÕconstËùÐÞÊεIJ¿·Ö½øÐг£Á¿»¯£¬ÈçÐβÎΪconst A* a£¬Ôò²»ÄܶԴ«µÝ½øÀ´µÄÖ¸ÕëµÄÄÚÈݽøÐиı䣬±£»¤ÁËÔ­Ö¸ÕëËùÖ¸ÏòµÄÄÚÈÝ£»ÈçÐβÎΪconst A& a£¬Ôò²»ÄܶԴ«µÝ½øÀ´µÄÒýÓöÔÏó½øÐиı䣬±£»¤ÁËÔ­¶ÔÏóµÄÊôÐÔ¡£

[×¢Òâ]£º

²ÎÊýconstͨ³£ÓÃÓÚ²ÎÊýΪָÕë»òÒýÓõÄÇé¿ö£¬ÇÒÖ»ÄÜÐÞÊÎÊäÈë²ÎÊý;ÈôÊäÈë²ÎÊý²ÉÓÓֵ´«µÝ”·½Ê½£¬ÓÉÓÚº¯Êý½«×Ô¶¯²úÉúÁÙʱ±äÁ¿ÓÃÓÚ¸´ÖƸòÎÊý£¬¸Ã²ÎÊý±¾¾Í²»ÐèÒª±£»¤£¬ËùÒÔ²»ÓÃconstÐÞÊΡ£

[×ܽá]£º

¶ÔÓÚ·ÇÄÚ²¿Êý¾ÝÀàÐ͵ÄÊäÈë²ÎÊý£¬Òò¸Ã½«“Öµ´«µÝ”µÄ·½Ê½¸ÄΪ“constÒýÓô«µÝ”£¬Ä¿µÄÊÇΪÁËÌá¸ßЧÂÊ¡£ÀýÈ磬½«void Func(A a)¸ÄΪvoid Func(const A &a)

¶ÔÓÚÄÚ²¿Êý¾ÝÀàÐ͵ÄÊäÈë²ÎÊý£¬²»Òª½«“Öµ´«µÝ”µÄ·½Ê½¸ÄΪ“constÒýÓô«µÝ”¡£·ñÔò¼È´ï²»µ½Ìá¸ßЧÂʵÄÄ¿µÄ£¬ÓÖ½µµÍÁ˺¯ÊýµÄ¿ÉÀí½âÐÔ¡£ÀýÈçvoid Func(int x)²»Ó¦¸Ã¸ÄΪvoid Func(const int &x)

2£© ÐÞÊηµ»ØÖµµÄconst£¬Èçconst A fun2( ); const A* fun3( );

ÕâÑùÉùÃ÷ÁË·µ»ØÖµºó£¬const°´ÕÕ"ÐÞÊÎÔ­Ôò"½øÐÐÐÞÊΣ¬Æðµ½ÏàÓ¦µÄ±£»¤×÷Óá£const Rational operator*(const Rational& lhs, const Rational& rhs)

{

return Rational(lhs.numerator() * rhs.numerator(),

lhs.denominator() * rhs.denominator());

}

·µ»ØÖµÓÃconstÐÞÊοÉÒÔ·ÀÖ¹ÔÊÐíÕâÑùµÄ²Ù×÷·¢Éú:Rational a,b;

Radional c;

(a*b) = c;

Ò»°ãÓÃconstÐÞÊηµ»ØֵΪ¶ÔÏó±¾Éí£¨·ÇÒýÓúÍÖ¸Õ룩µÄÇé¿ö¶àÓÃÓÚ¶þÄ¿²Ù×÷·ûÖØÔغ¯Êý²¢²úÉúжÔÏóµÄʱºò¡£

[×ܽá]£º

£¨1£©Ò»°ãÇé¿öÏ£¬º¯ÊýµÄ·µ»ØֵΪij¸ö¶ÔÏóʱ£¬Èç¹û½«ÆäÉùÃ÷Ϊconstʱ£¬¶àÓÃÓÚ²Ù×÷·ûµÄÖØÔØ¡£Í¨³££¬²»½¨ÒéÓÃconstÐÞÊκ¯ÊýµÄ·µ»ØÖµÀàÐÍΪij¸ö¶ÔÏó»ò¶Ôij¸ö¶ÔÏóÒýÓõÄÇé¿ö¡£Ô­ÒòÈçÏ£ºÈç¹û·µ»ØֵΪij¸ö¶ÔÏóΪconst£¨const A test = A ʵÀý£©»òij¸ö¶ÔÏóµÄÒýÓÃΪconst£¨const A& test = AʵÀý£© £¬Ôò·µ»ØÖµ¾ßÓÐconstÊôÐÔ£¬Ôò·µ»ØʵÀýÖ»ÄÜ·ÃÎÊÀàAÖеĹ«ÓУ¨±£»¤£©Êý¾Ý³ÉÔ±ºÍconst³ÉÔ±º¯Êý£¬²¢ÇÒ²»ÔÊÐí¶ÔÆä½øÐи³Öµ²Ù×÷£¬ÕâÔÚÒ»°ãÇé¿öϺÜÉÙÓõ½¡£

£¨2£©Èç¹û¸ø²ÉÓÓָÕë´«µÝ”·½Ê½µÄº¯Êý·µ»ØÖµ¼ÓconstÐÞÊΣ¬ÄÇôº¯Êý·µ»ØÖµ£¨¼´Ö¸Õ룩µÄÄÚÈݲ»Äܱ»Ð޸ģ¬¸Ã·µ»ØÖµÖ»Äܱ»¸³¸ø¼Óconst ÐÞÊεÄͬÀàÐÍÖ¸Õë¡£È磺

const char * GetString(void);

ÈçÏÂÓï¾ä½«³öÏÖ±àÒë´íÎó£º

char *str=GetString();

ÕýÈ·µÄÓ÷¨ÊÇ£º

const char *str=GetString();

£¨3£©º¯Êý·µ»ØÖµ²ÉÓÓÒýÓô«µÝ”µÄ³¡ºÏ²»¶à£¬ÕâÖÖ·½Ê½Ò»°ãÖ»³öÏÖÔÚÀàµÄêçÖµº¯ÊýÖУ¬Ä¿µÄÊÇΪÁËʵÏÖÁ´Ê½±í´ï¡£È磺

class A

{…

A &operate = (const A &other); //¸³Öµº¯Êý

}

A a,b,c; //a,b,cΪAµÄ¶ÔÏó

a=b=c; //Õý³£

(a=b)=c; //²»Õý³££¬µ«ÊǺϷ¨

Èô¸³Öµº¯ÊýµÄ·µ»ØÖµ¼ÓconstÐÞÊΣ¬ÄÇô¸Ã·µ»ØÖµµÄÄÚÈݲ»ÔÊÐíÐ޸ģ¬ÉÏÀýÖÐa=b=cÒÀÈ»ÕýÈ·¡£(a=b)=c¾Í²»ÕýÈ·ÁË¡£

[˼¿¼3]£º ÕâÑù¶¨Ò帳ֵ²Ù×÷·ûÖØÔغ¯Êý¿ÉÒÔÂð£¿

const A& perator=(const A& a);

2. Àà³ÉÔ±º¯ÊýÖÐconstµÄʹÓÃ

Ò»°ã·ÅÔÚº¯ÊýÌåºó£¬ÐÎÈ磺void fun() const;

Èκβ»»áÐÞ¸ÄÊý¾Ý³ÉÔ±µÄº¯Êý¶¼Òò¸ÃÉùÃ÷ΪconstÀàÐÍ¡£Èç¹ûÔÚ±àдconst³ÉÔ±º¯Êýʱ£¬²»É÷ÐÞ¸ÄÁËÊý¾Ý³ÉÔ±£¬»òÕßµ÷ÓÃÁËÆäËû·Çconst³ÉÔ±º¯Êý£¬±àÒëÆ÷½«±¨´í£¬Õâ´ó´óÌá¸ßÁ˳ÌÐòµÄ½¡×³ÐÔ¡£È磺

class Stack

{

public:

void Push(int elem);

int Pop(void);

int GetCount(void) const; //const ³ÉÔ±º¯Êý

private:

int m_num;

int m_data[100];

};

int Stack::GetCount(void) const

{

++m_num; //±àÒë´íÎó£¬ÆóͼÐÞ¸ÄÊý¾Ý³ÉÔ±m_num

Pop(); //±àÒë´íÎó£¬Æóͼµ÷Ó÷Çconstº¯Êý

Return m_num;

}

3. ʹÓÃconstµÄһЩ½¨Òé

£¨1£©Òª´óµ¨µÄʹÓÃconst£¬Õ⽫¸øÄã´øÀ´ÎÞ¾¡µÄÒæ´¦£¬µ«Ç°ÌáÊÇÄã±ØÐë¸ãÇå³þԭί£»

£¨2£©Òª±ÜÃâ×îÒ»°ãµÄ¸³Öµ²Ù×÷´íÎó£¬È罫const±äÁ¿¸³Öµ£¬¾ßÌå¿É¼û˼*£»

£¨3£©ÔÚ²ÎÊýÖÐʹÓÃconstÓ¦¸ÃʹÓÃÒýÓûòÖ¸Õ룬¶ø²»ÊÇÒ»°ãµÄ¶ÔÏóʵÀý£¬Ô­ÒòͬÉÏ£»

£¨4£© constÔÚ³ÉÔ±º¯ÊýÖеÄÈýÖÖÓ÷¨£¨²ÎÊý¡¢·µ»ØÖµ¡¢º¯Êý£©ÒªºÜºÃµÄʹÓã»

£¨5£© ²»ÒªÇáÒ׵Ľ«º¯ÊýµÄ·µ»ØÖµÀàÐͶ¨Îªconst;

£¨6£©³ýÁËÖØÔزÙ×÷·ûÍâÒ»°ã²»Òª½«·µ»ØÖµÀàÐͶ¨Îª¶Ôij¸ö¶ÔÏóµÄconstÒýÓÃ;

×÷ÕߣºÌÕÄþ,»ªÇåÔ¶¼ûǶÈëʽѧԺÉϺ£ÖÐÐĽ²Ê¦¡£ À´Ô´£ºÎ¬¿â¿ª·¢Íø


΢ÐÅɨÃè·ÖÏí±¾Îĵ½ÅóÓÑȦ
ɨÂë¹Ø×¢5GͨÐŹٷ½¹«ÖÚºÅ,Ãâ·ÑÁìÈ¡ÒÔÏÂ5G¾«Æ·×ÊÁÏ
  • 1¡¢»Ø¸´¡°YD5GAI¡±Ãâ·ÑÁìÈ¡¡¶ÖйúÒƶ¯£º5GÍøÂçAIÓ¦ÓõäÐͳ¡¾°¼¼Êõ½â¾ö·½°¸°×ƤÊé¡·
  • 2¡¢»Ø¸´¡°5G6G¡±Ãâ·ÑÁìÈ¡¡¶5G_6GºÁÃײ¨²âÊÔ¼¼Êõ°×ƤÊé-2022_03-21¡·
  • 3¡¢»Ø¸´¡°YD6G¡±Ãâ·ÑÁìÈ¡¡¶ÖйúÒƶ¯£º6GÖÁ¼òÎÞÏß½ÓÈëÍø°×ƤÊé¡·
  • 4¡¢»Ø¸´¡°LTBPS¡±Ãâ·ÑÁìÈ¡¡¶¡¶ÖйúÁªÍ¨5GÖն˰×ƤÊé¡·¡·
  • 5¡¢»Ø¸´¡°ZGDX¡±Ãâ·ÑÁìÈ¡¡¶ÖйúµçÐÅ5GNTN¼¼Êõ°×ƤÊé¡·
  • 6¡¢»Ø¸´¡°TXSB¡±Ãâ·ÑÁìÈ¡¡¶Í¨ÐÅÉ豸°²×°¹¤³ÌÊ©¹¤¹¤ÒÕͼ½â¡·
  • 7¡¢»Ø¸´¡°YDSL¡±Ãâ·ÑÁìÈ¡¡¶ÖйúÒƶ¯ËãÁ¦²¢Íø°×ƤÊé¡·
  • 8¡¢»Ø¸´¡°5GX3¡±Ãâ·ÑÁìÈ¡¡¶R1623501-g605GµÄϵͳ¼Ü¹¹1¡·
  • ±¾ÖÜÈȵ㱾ÔÂÈȵã

     

      ×îÈÈͨÐÅÕÐƸ

    Òµ½ç×îÐÂ×ÊѶ


      ×îÐÂÕÐƸÐÅÏ¢

    ×îм¼ÊõÎÄÕÂ

    ×îÐÂÂÛ̳Ìù×Ó