멤버 변수의 선언 정의?

dragonHu의 이미지

class A
{
public:
int a;
static int b;
};
//int A::b;

int main()
{
A::b;
}

에서 LNK2001 에러가 나는 것을 해결하면서 멤버변수의 선언과 정의에 대해서 의문점을 가지게되었는데요

저는 int a; static int b; 둘다 선언이고 a는 문법상 클래스가 객체화 되고 나서야 참조 할 수 있어 LNK2001 같은 링크오류가 뜰 방도가 없다.
하지만, static 변수는 클래스가 객체화 되지 않아도 A::b와 같이 참조 할 수 있어 정의가 없다는 오류가 발생할 수 있으니 int A::b; 이런식으로 정의를 해주어야 한다.
이렇게 결론을 내렸는데 좀 더 찾아보니 int a 는 정의, static int b 는 선언이라 하더군요

결론은 static int b 는 선언이면서 왜 int a 는 정의인지 알고싶습니다.

twinwings의 이미지

C/C++ 언어에는

함수에서 선언과 정의가 있는 것 처럼

변수에도 선언과 정의를 따로 할 수 있습니다.

지역변수의 경우 보통 선언과 정의를 묶어서 하나로 하죠.

전역변수의 경우, 명시적으로 선언을 할 경우 보통 extern 이라는 키워드를 씁니다.

예를 들어 a.h 안에 다음과 같은 선언이 있다고 합시다.

extern int global_value;

그리고 이 헤더파일에 대응되는 구현부에 다음과 같이 global_value가 정의되어 있습니다.

int global_value;

이 헤더 파일을 여러곳에서 include해서 global_value 변수를 수정한다고 할 때

서로 다른 파일이더라도 global_value 변수는 동일한 변수일겁니다.

그러면, global_value가 실제로 존재하는 메모리는 어디어야 하죠?

이것을 명시해주는 것이 변수의 "정의" 입니다.

만약 헤더파일에 전역변수를 "정의" 해버린다면, include 할 때 마다

동일한 이름의 전역변수가 메모리에 올라가게 됩니다. 즉, 논리적으로 1개인 전역변수가

메모리 상에 여러개가 존재하게 되죠. 말이안되는겁니다.

그렇기 때문에 전역변수의 경우에는 반드시

"선언"과 "정의"를 따로 해주어야 합니다.

예외로, C++에서는 전역 상수인 경우 선언과 동시에 정의를 해도 됩니다.
(C와는 구별되는 다른점입니다.)

(설명의 편의상 전역변수로 설명했습니다. 세세한 부분이 틀린데가 있으니
자세한건 static, global, linkage 등의 키워드로 검색해보세요.)

dragonHu의 이미지

깔끔하게 해결 됬네요 고맙습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.