다음을 통해 공유


const(C++)

데이터 선언을 수정할 때 const 키워드는 개체나 변수를 수정할 수 없음을 지정합니다.

구문

:
ptr-declarator
noptr-declarator parameters-and-qualifiers trailing-return-type
:
noptr-declarator
ptr-operator ptr-declarator
:
declarator-id attribute-specifier-seqopt
noptr-declarator parameters-and-qualifiers
noptr-declarator [ constant-expressionopt ] attribute-specifier-seqopt
( ptr-declarator )
:
( parameter-declaration-clause ) cv-qualifier-seqopt
ref-qualifieropt noexcept-specifieropt attribute-specifier-seqopt
:
-> type-id
:
* attribute-specifier-seqopt cv-qualifier-seqopt
& attribute-specifier-seqopt
&& attribute-specifier-seqopt
nested-name-specifier * attribute-specifier-seqopt cv-qualifier-seqopt
:
cv-qualifier cv-qualifier-seqopt
:
const
volatile
:
&
&&
:
...opt id-expression

const

const 키워드는 변수의 값이 상수임을 지정하고 프로그래머가 이 변수를 수정하지 못하게 하도록 컴파일러에 지시합니다.

// constant_values1.cpp
int main() {
   const int i = 5;
   i = 10;   // C3892
   i++;   // C2105
}

C++에서는 #define 전처리기 지시문 대신 const 키워드를 사용하여 상수 값을 정의할 수 있습니다. const로 정의된 값은 형식 검사를 받으며 상수 식 대신 사용할 수 있습니다. C++에서는 다음과 같이 const 변수를 사용하여 배열의 크기를 지정할 수 있습니다.

// constant_values2.cpp
// compile with: /c
const int maxarray = 255;
char store_char[maxarray];  // allowed in C++; not allowed in C

C에서 상수 값은 기본적으로 외부 링크로 설정되므로 소스 파일에만 나타날 수 있습니다. C++에서 상수 값은 기본적으로 내부 링크로 설정되므로 헤더 파일에 나타날 수 있습니다.

const 키워드는 포인터 선언에도 사용할 수 있습니다.

// constant_values3.cpp
int main() {
   char this_char{'a'}, that_char{'b'};
   char *mybuf = &this_char, *yourbuf = &that_char;
   char *const aptr = mybuf;
   *aptr = 'c';   // OK
   aptr = yourbuf;   // C3892
}

const로 선언된 변수에 대한 포인터는 const로 선언된 포인터에만 할당될 수 있습니다.

// constant_values4.cpp
#include <stdio.h>
int main() {
   const char *mybuf = "test";
   char *yourbuf = "test2";
   printf_s("%s\n", mybuf);

   const char *bptr = mybuf;   // Pointer to constant data
   printf_s("%s\n", bptr);

   // *bptr = 'a';   // Error
}

상수 데이터에 대한 포인터를 함수 매개 변수로 사용하여 함수가 포인터를 통해 전달된 매개 변수를 수정하지 못하게 할 수 있습니다.

const로 선언된 개체의 경우 상수 멤버 함수만 호출할 수 있습니다. 컴파일러는 상수 개체가 수정되지 않도록 보장합니다.

birthday.getMonth();    // Okay
birthday.setMonth( 4 ); // Error

상수가 아닌 개체에 대해 상수 또는 상수가 아닌 멤버 함수를 호출할 수 있습니다. const 키워드를 사용하여 멤버 함수를 오버로드할 수도 있으며, 이 기능에 따라 상수 및 비상수 개체에 대해 다른 버전의 함수를 호출할 수 있습니다.

const 키워드를 사용하여 생성자나 소멸자를 선언할 수 없습니다.

const 멤버 함수

const 키워드로 멤버 함수를 선언하면 함수가 자신이 호출되는 개체를 수정하지 않는 "읽기 전용" 함수로 지정됩니다. 상수 멤버 함수는 비정적 데이터 멤버를 수정하거나 상수가 아닌 멤버 함수를 호출할 수 없습니다. 상수 멤버 함수를 선언하려면 인수 목록의 닫는 괄호 뒤에 const 키워드를 배치합니다. 선언과 정의 모두에 const 키워드가 필요합니다.

// constant_member_function.cpp
class Date
{
public:
   Date( int mn, int dy, int yr );
   int getMonth() const;     // A read-only function
   void setMonth( int mn );   // A write function; can't be const
private:
   int month;
};

int Date::getMonth() const
{
   return month;        // Doesn't modify anything
}
void Date::setMonth( int mn )
{
   month = mn;          // Modifies data member
}
int main()
{
   Date MyDate( 7, 4, 1998 );
   const Date BirthDate( 1, 18, 1953 );
   MyDate.setMonth( 4 );    // Okay
   BirthDate.getMonth();    // Okay
   BirthDate.setMonth( 4 ); // C2662 Error
}

C와 C++ const 차이점

C 소스 코드 파일에서 const 변수를 정의하는 경우 다음과 같이 수행합니다.

const int i = 2;

그런 다음 이 변수를 아래와 같이 다른 모듈에서 사용할 수 있습니다.

extern const int i;

그러나 C++에서 동일한 동작을 가져오려면 const 변수를 다음과 같이 정의해야 합니다.

extern const int i = 2;

C와 마찬가지로 이 변수를 다음과 같이 다른 모듈에서 사용할 수 있습니다.

extern const int i;

C 소스 코드 파일에서 사용하기 위해 C++ 소스 코드 파일에 extern 변수를 정의하려면 다음을 사용합니다.

extern "C" const int x=10;

C++ 컴파일러에 의한 이름 변환을 방지해야 합니다.

설명

멤버 함수의 매개 변수 목록을 따를 때 const 키워드는 함수가 자신이 호출되는 개체를 수정하지 않도록 지정합니다.

const에 대한 자세한 내용은 다음 문서를 참조하세요.

참고 항목

키워드