2017-03-01 1 views
0

C++에서 주 프로그램 뒤에 struct를 정의 할 수 있습니까? 함수를 정의 할 때 주 프로그램 앞에 함수를 선언 한 다음 주 함수 뒤에 함수 정의를 쓸 수 있습니다. 구조를 정의 할 때 이와 비슷한 것을 할 수 있는지 알고 싶었습니다. 고맙습니다.C++에서 main 함수 뒤에 struct를 정의 할 수 있습니까?

+7

왜 시도하지 않습니까? – Netwave

+0

네, 그렇게 할 수 있습니다. 조회 _forward 선언 _. –

+0

http : //en.cppreference.com/w/cpp/language/class # Forward_declaration –

답변

4

C++에서 주 프로그램 뒤에 struct를 정의 할 수 있습니까?

나는 main 기능을 의미한다고 가정합니다. 예, main 함수 뒤에 클래스 (구조체 포함)를 정의 할 수 있습니다. 데모 :

int main(){} 
struct S{}; 

우리는 함수를 정의

, 우리는 메인 프로그램하기 전에 함수를 선언 한 후 메인 프로그램 후 함수 정의를 작성할 수 있습니다. 구조를 정의 할 때 이와 비슷한 것을 할 수 있는지 알고 싶었습니다.

같은 것을 클래스에 적용하면 함수 앞에 선언 할 수 있고 이후에 정의 할 수 있습니다. 그러나 불완전한 (선언되었지만 정의되지 않은) 클래스의 사용은 매우 제한적입니다. 포인터와 포인터를 정의 할 수는 있지만 포인터를 만들거나 멤버 함수를 호출 할 수는 없습니다. 데모 :

struct S;  // (forward) declaration of a class 
S* factory(); // (forward) declaration of a function 
int main(){ 
    S* s = factory(); // OK, no definition required 
    // s->foo();  // not OK, S is incomplete 
    // S s2;   // not OK 
} 
struct S{    // definition of a class 
    void foo();  // declaration of a member function 
}; 
S* factory() { 
    static S s;  // OK, S is complete 
    s.foo();   // OK, note how the member function can be called 
         // before it is defined, just like free functions 
    return &s; 
} 
void S::foo() {}  // definition of a member function 
3

편집 : @ user2079303 주석에서 언급 한대로 'forward declaration'이라는 용어를 잘못 사용했습니다. 그에 따라 내 대답을 업데이 트되었습니다.

구조체에 대한 포인터 만 저장하려는 경우 구조체를 전달 선언 할 수 있습니다. 그러나 일단 구조체가 정의되면 해당 포인터의 메소드 만 호출 할 수 있습니다.

#include <iostream> 

// forward declaration of struct 
struct S; 
// pointer can be defined after forward declaration 
S * s; 

void workOnSPointer(); 

int main() 
{ 
    workOnSPointer(); 
} 

// definition of struct 
struct S 
{ 
    S() : bar(42) {} 
    void foo() { std::cout << "bar is " << bar << "\n"; } 
    int bar; 
}; 

// methods can only be called after definition 
void workOnSPointer() 
{ 
    S * s = new S(); 
    s->foo(); 
    delete s; 
} 

나중에 구조체 정의에서 구조체의 메소드를 전달 선언 할 수 있습니다.

#include <iostream> 

// incomplete definition of struct S 
// methods of the struct are only declared here 
struct S 
{ 
    S(); 
    void foo(); 
    int bar; 
}; 

int main() 
{ 
    S s; 
    s.foo(); 
} 

// definition of the struct's methods 
S::S() : bar(42) {} 
void S::foo() { std::cout << "bar is " << bar << "\n"; } 

전달 정의 된 메소드를 사용한이 정의는 헤더 (.h) 파일의 주요 용도입니다. 헤더에 구조체를 정의하고 구조체가 사용되는 파일의 헤더를 포함 할 수 있습니다. 메소드의 정의는 소스 (.cpp) 파일로 이동합니다. 코드에 적용이 의미 위 :

파일 S.h

struct S 
{ 
    S(); 
    void foo(); 
    int bar; 
}; 

파일 S.cpp

#include "S.h" 
#include <iostream> 

S::S() : bar(42) {} 
void S::foo() { std::cout << "bar is " << bar << "\n"; } 

파일 main.cpp

#include "S.h" 

int main() 
{ 
    S s; 
    s.foo(); 
} 

당신이 S.cppmain.cpp하고 리 컴파일하는 경우 결과 객체 파일을 nk하면 처음부터 코드와 동일한 동작을 얻게됩니다.

+0

@ user2079303이 맞으면 포인터를 정의하는 데 forward 선언을 사용할 수 있습니다. –

+0

'struct'가 선언 된 또 다른 샘플 : http://coliru.stacked-crooked.com/a/8789088991c57916 –

+2

'void main()'이 정확하지 않습니다.'int main()'이어야합니다. – mch

관련 문제