클래스 코드를 헤더와 cpp 파일로 분리
간단한 클래스의 구현 및 선언 코드를 새로운 헤더 및 cpp 파일로 분리하는 방법에 대해 혼란스러워합니다. 예를 들어 다음 클래스의 코드를 어떻게 분리합니까?
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y)
{
gx = x;
gy = y;
}
int getSum()
{
return gx + gy;
}
};
클래스 선언은 헤더 파일에 들어갑니다. #ifndef
포함 가드 를 추가 하거나 MS 플랫폼에있는 경우 사용할 수도 있습니다 #pragma once
. 또한 개인을 생략했습니다. 기본적으로 C ++ 클래스 멤버는 개인입니다.
// A2DD.h
#ifndef A2DD_H
#define A2DD_H
class A2DD
{
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
#endif
구현은 CPP 파일로갑니다.
// A2DD.cpp
#include "A2DD.h"
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
일반적으로 .h에는 모든 데이터와 모든 메소드 선언 인 클래스 정의가 포함됩니다. 귀하의 경우에 이와 같이 :
A2DD.h:
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
그리고 .cpp에는 다음과 같은 메소드의 구현이 포함되어 있습니다.
A2DD.cpp:
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
기본적으로 함수 선언 / 정의의 수정 된 구문 :
a2dd.h
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
a2dd.cpp
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
A2DD.h
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
A2DD.cpp
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
The idea is to keep all function signatures and members in the header file.
This will allow other project files to see how the class looks like without having to know the implementation.
And besides that, you can then include other header files in the implementation instead of the header. This is important because whichever headers are included in your header file will be included (inherited) in any other file that includes your header file.
It's important to point out to readers stumbling upon this question when researching the subject in a broader fashion that the accepted answer's procedure is not required in the case you just want to split your project into files. It's only needed when you need multiple implementations of single classes. If your implementation per class is one, just one header file for each is enough.
Hence, from the accepted answer's example only this part is needed:
#ifndef MYHEADER_H
#define MYHEADER_H
//Class goes here, full declaration AND implementation
#endif
The #ifndef etc. preprocessor definitions allow it to be used multiple times.
PS. The topic becomes clearer once you realize C/C++ is 'dumb' and #include is merely a way to say "dump this text at this spot".
You leave the declarations in the header file:
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y); // leave the declarations here
int getSum();
};
And put the definitions in the implementation file.
A2DD::A2DD(int x,int y) // prefix the definitions with the class name
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
You could mix the two (leave getSum()
definition in the header for instance). This is useful since it gives the compiler a better chance at inlining for example. But it also means that changing the implementation (if left in the header) could trigger a rebuild of all the other files that include the header.
Note that for templates, you need to keep it all in the headers.
Usually you put only declarations and really short inline functions in the header file:
For instance:
class A {
public:
A(); // only declaration in the .h unless only a short initialization list is used.
inline int GetA() const {
return a_;
}
void DoSomethingCoplex(); // only declaration
private:
int a_;
};
참고URL : https://stackoverflow.com/questions/9579930/separating-class-code-into-a-header-and-cpp-file
'IT' 카테고리의 다른 글
생성자 함수와 프로토 타입에서 자바 스크립트 객체 메소드 선언 (0) | 2020.06.16 |
---|---|
자바 스크립트로 웹 페이지의 스크린 샷을 찍으시겠습니까? (0) | 2020.06.16 |
Razor를 사용하여 인코딩되지 않은 Json을 View에 작성하는 방법 (0) | 2020.06.15 |
std :: map을 반복하는 순서가 알려져 있습니까 (그리고 표준에 의해 보장됨)? (0) | 2020.06.15 |
MySQL이 "데이터 전송"상태에있을 때 무슨 의미입니까? (0) | 2020.06.15 |