[C++] 클래스 내부에서의 헤더파일 인클루드에 관해 질문있습니다.

khnet의 이미지

class TEST
{
  public:
    #include <test.h>
};
 
TEST A;

// test.h
 
typedef testtype TESTTYPE;
int a;  // 전역 선언

위와 같은 코드가 있을 때 정의된 타입은 TEST::TESTTYPE으로 표현이 되던데요
그럼 변수 a도 TEST::a 또는 A.a로 표현되나요?

만약 그렇다면 a는 TEST 클래스의 스테틱 멤버변수 또는 멤버변수로 취급되는 건가요?

알려주시면 감사드리겠습니다.

HDNua의 이미지

헤더 파일을 포함하는 #include 구문은, 코드 자체가 뭉텅이로 대치되는 것이니 문제는 없습니다.
극단적인 예로 다음의 코드도 정상적으로 동작하지요.
header1.h

int main()
{

header2.h
    return 0;
}

main.cpp
#include "header1.h"
#include "header2.h"

따라서 이 부분은 그냥 헤더 파일의 내용으로 대치했습니다.

#include <iostream>
using namespace std;
 
class Test
{
public:
    typedef unsigned int Type;
    int pub_var;
};
 
int main(void)
{
    Test::Type tvar1;   // No error
    Test::pub_var = 1;  // error C2597
    Type tvar2;         // error C2065
 
    Test tobj1;
    tobj1.Type tvar2;   // error C2274
    tobj1.pub_var = 2;  // No error
 
    return 0;
}

일단 객체 object의 a Test 인스턴스의 멤버 변수이므로 object.a로 표현해야 옳습니다.

아래는 오류 메시지에 대한 자세한 설명입니다.
error C2597: 비정적 멤버 'Test::pub_var'에 대한 참조가 잘못되었습니다.
error C2065: 'Type' : 선언되지 않은 식별자입니다.
error C2274: '함수 스타일 캐스트' : '.' 연산자의 오른쪽에 사용할 수 없습니다.

아래는 이에 대한 MSDN의 도움말인데, 이 중 C2274의 설명이 재미있군요.
error C2597: http://msdn.microsoft.com/ko-kr/library/422tf4a2(VS.90).aspx
error C2274: http://msdn.microsoft.com/ko-kr/library/kst1dcx0(VS.90).aspx

저는 이렇게 생각했습니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.