C++ struct vs class | 접근 제어·POD·C 호환 완벽 비교
이 글의 핵심
C++ struct vs class 차이는 기본 public/private뿐, 기능은 동일합니다. 데이터 묶음 vs 캡슐화 관례, POD·C 호환까지 문법은 같고 의도 표현과 선택 기준을 비교합니다.
들어가며
C++에서 struct와 class는 기본 접근 제어만 다를 뿐, 기능은 완전히 동일합니다.
비유로 말씀드리면, 문법상 차이는 회의실 문 앞에 ‘공개’ 스티커를 붙이느냐, ‘비공개’가 기본이냐 정도이고, 방 안에서 할 수 있는 일(메서드, 상속 등)은 같습니다. 관례적으로는 데이터 묶음에는 struct, 불변식을 지키는 객체에는 class를 쓰는 경우가 많습니다.
이 글을 읽으면
- struct와 class의 유일한 차이를 이해합니다
- 사용 규칙과 선택 기준을 파악합니다
- POD 타입과 C 호환성을 확인합니다
- 실무 시나리오별 선택 전략을 익힙니다
실전 경험에서 배운 교훈
이 기술을 실무 프로젝트에 처음 도입했을 때, 공식 문서만으로는 알 수 없었던 많은 함정들이 있었습니다. 특히 프로덕션 환경에서 발생하는 엣지 케이스들은 로컬 개발 환경에서는 재현조차 되지 않았죠.
가장 어려웠던 점은 성능 최적화였습니다. 처음엔 “동작만 하면 되겠지”라고 생각했지만, 실제 사용자 트래픽이 몰리면서 병목 지점들이 하나씩 드러났습니다. 특히 데이터베이스 쿼리 최적화, 캐싱 전략, 에러 핸들링 구조 등은 여러 번의 장애를 겪으면서 개선해 나갔습니다.
이 글에서는 그런 시행착오를 통해 얻은 실전 노하우와, “이렇게 하면 안 된다”는 교훈들을 함께 정리했습니다. 특히 트러블슈팅 섹션은 실제 장애 대응 경험을 바탕으로 작성했으니, 비슷한 문제를 마주했을 때 참고하시면 도움이 될 것입니다.
struct vs class 차이
유일한 차이: 기본 접근 제어
| 항목 | struct | class |
|---|---|---|
| 기본 접근 제어 | public | private |
| 기본 상속 | public | private |
| 생성자 | ✅ | ✅ |
| 소멸자 | ✅ | ✅ |
| 멤버 함수 | ✅ | ✅ |
| 가상 함수 | ✅ | ✅ |
| 상속 | ✅ | ✅ |
| 템플릿 | ✅ | ✅ |
실전 구현
1) 기본 접근 제어
#include <iostream>
// struct: 기본 public
struct Point {
int x, y; // public (기본)
};
// class: 기본 private
class Point2 {
int x, y; // private (기본)
public:
Point2(int x, int y) : x(x), y(y) {}
int getX() const { return x; }
int getY() const { return y; }
};
int main() {
Point p;
p.x = 10; // ✅ OK
p.y = 20;
Point2 p2(10, 20);
// p2.x = 10; // ❌ 컴파일 에러: private 멤버
std::cout << p2.getX() << std::endl;
return 0;
}
2) 상속 기본 접근 제어
#include <iostream>
class Base {
public:
void foo() {
std::cout << "Base::foo" << std::endl;
}
};
// struct: 기본 public 상속
struct DerivedStruct : Base { // public 상속
};
// class: 기본 private 상속
class DerivedClass : Base { // private 상속
};
int main() {
DerivedStruct ds;
ds.foo(); // ✅ OK (public 상속)
DerivedClass dc;
// dc.foo(); // ❌ 컴파일 에러 (private 상속)
return 0;
}
3) struct도 class처럼 사용 가능
#include <iostream>
struct MyStruct {
private: // private 명시 가능
int x_;
public:
MyStruct(int x) : x_(x) {
std::cout << "생성자: " << x_ << std::endl;
}
virtual void foo() { // 가상 함수
std::cout << "MyStruct::foo: " << x_ << std::endl;
}
virtual ~MyStruct() {
std::cout << "소멸자: " << x_ << std::endl;
}
};
struct Derived : MyStruct {
Derived(int x) : MyStruct(x) {}
void foo() override {
std::cout << "Derived::foo" << std::endl;
}
};
int main() {
MyStruct* p = new Derived(42);
p->foo(); // Derived::foo
delete p;
return 0;
}
4) 사용 규칙 (Google C++ Style Guide)
struct: 데이터만 담는 수동적 객체
// ✅ struct 사용
struct Point {
int x, y;
};
struct Color {
uint8_t r, g, b, a;
};
struct Config {
std::string host;
int port;
bool useSSL;
};
class: 캡슐화와 메서드가 있는 능동적 객체
// ✅ class 사용
class BankAccount {
private:
double balance_;
public:
BankAccount(double initial) : balance_(initial) {}
void deposit(double amount) {
if (amount > 0) {
balance_ += amount;
}
}
void withdraw(double amount) {
if (amount > 0 && balance_ >= amount) {
balance_ -= amount;
}
}
double getBalance() const {
return balance_;
}
};
고급 활용
1) POD 타입
POD(Plain Old Data)는 C와 호환되는 단순 타입입니다. POD 조건 (C++11):
- Trivial 생성자
- Trivial 소멸자
- Trivial 복사/이동 연산자
- Standard layout (private/protected 멤버 없음, 가상 함수 없음)
#include <iostream>
#include <type_traits>
// ✅ POD
struct Point {
int x, y;
};
static_assert(std::is_pod_v<Point>); // true
// ❌ 비POD (생성자 있음)
struct Point2 {
int x, y;
Point2(int x, int y) : x(x), y(y) {}
};
static_assert(!std::is_pod_v<Point2>); // false
int main() {
std::cout << "Point is POD: " << std::is_pod_v<Point> << std::endl;
std::cout << "Point2 is POD: " << std::is_pod_v<Point2> << std::endl;
return 0;
}
2) C 호환성
// common.h
#ifdef __cplusplus
extern "C" {
#endif
struct Point {
int x, y;
};
void processPoint(struct Point* p);
#ifdef __cplusplus
}
#endif
// common.cpp
#include "common.h"
#include <iostream>
void processPoint(struct Point* p) {
std::cout << "Point: (" << p->x << ", " << p->y << ")" << std::endl;
}
// main.cpp
#include "common.h"
int main() {
Point p = {10, 20}; // C++에서는 struct 키워드 생략 가능
processPoint(&p);
return 0;
}
3) memcpy 가능 (POD)
#include <cstring>
#include <iostream>
struct Point {
int x, y;
};
int main() {
Point p1 = {10, 20};
Point p2;
std::memcpy(&p2, &p1, sizeof(Point)); // ✅ POD는 memcpy 가능
std::cout << "p2: (" << p2.x << ", " << p2.y << ")" << std::endl;
return 0;
}
성능 비교
struct vs class
#include <chrono>
#include <iostream>
struct PointStruct {
int x, y;
};
class PointClass {
public:
int x, y;
};
int main() {
auto start1 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 10000000; ++i) {
PointStruct p = {i, i};
int sum = p.x + p.y;
}
auto end1 = std::chrono::high_resolution_clock::now();
auto time1 = std::chrono::duration_cast<std::chrono::milliseconds>(end1 - start1).count();
auto start2 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 10000000; ++i) {
PointClass p = {i, i};
int sum = p.x + p.y;
}
auto end2 = std::chrono::high_resolution_clock::now();
auto time2 = std::chrono::duration_cast<std::chrono::milliseconds>(end2 - start2).count();
std::cout << "struct: " << time1 << "ms" << std::endl;
std::cout << "class: " << time2 << "ms" << std::endl;
return 0;
}
결과 (GCC 13, -O3):
| 타입 | 시간 | 상대 속도 |
|---|---|---|
| struct | 5ms | 1.0x |
| class | 5ms | 1.0x |
| 결론: 성능 차이 없음 |
실무 사례
사례 1: 게임 엔진 - 데이터 구조
#include <iostream>
#include <vector>
// struct: 데이터만
struct Transform {
float x, y, z;
float rotX, rotY, rotZ;
float scaleX, scaleY, scaleZ;
};
// class: 캡슐화
class Entity {
private:
int id_;
Transform transform_;
bool active_;
public:
Entity(int id) : id_(id), transform_{0, 0, 0, 0, 0, 0, 1, 1, 1}, active_(true) {}
void setPosition(float x, float y, float z) {
transform_.x = x;
transform_.y = y;
transform_.z = z;
}
Transform getTransform() const {
return transform_;
}
bool isActive() const {
return active_;
}
};
int main() {
Entity entity(1);
entity.setPosition(10.0f, 20.0f, 30.0f);
Transform t = entity.getTransform();
std::cout << "Position: (" << t.x << ", " << t.y << ", " << t.z << ")" << std::endl;
return 0;
}
사례 2: 네트워크 - 프로토콜 메시지
#include <cstring>
#include <iostream>
// struct: 네트워크 메시지 (POD)
struct Message {
uint32_t type;
uint32_t length;
char data[256];
};
// class: 메시지 핸들러
class MessageHandler {
public:
void handleMessage(const Message& msg) {
std::cout << "Type: " << msg.type << ", Length: " << msg.length << std::endl;
std::cout << "Data: " << msg.data << std::endl;
}
};
int main() {
Message msg;
msg.type = 1;
msg.length = 5;
std::strncpy(msg.data, "Hello", 255);
msg.data[255] = '\0';
MessageHandler handler;
handler.handleMessage(msg);
return 0;
}
사례 3: 데이터베이스 - DTO
#include <iostream>
#include <string>
#include <vector>
// struct: DTO (Data Transfer Object)
struct UserDTO {
int id;
std::string name;
std::string email;
int age;
};
// class: 서비스
class UserService {
public:
UserDTO getUserById(int id) {
// 데이터베이스 조회
return {id, "홍길동", "[email protected]", 30};
}
void saveUser(const UserDTO& user) {
std::cout << "저장: " << user.name << std::endl;
}
};
int main() {
UserService service;
UserDTO user = service.getUserById(1);
std::cout << "이름: " << user.name << std::endl;
user.age = 31;
service.saveUser(user);
return 0;
}
사례 4: 설정 관리
#include <iostream>
#include <string>
// struct: 설정 데이터
struct ServerConfig {
std::string host;
int port;
int maxConnections;
bool useSSL;
};
// class: 설정 관리자
class ConfigManager {
private:
ServerConfig config_;
public:
ConfigManager(const ServerConfig& config) : config_(config) {}
void validate() {
if (config_.port < 1 || config_.port > 65535) {
throw std::invalid_argument("잘못된 포트 번호");
}
if (config_.maxConnections < 1) {
throw std::invalid_argument("잘못된 최대 연결 수");
}
}
ServerConfig getConfig() const {
return config_;
}
};
int main() {
ServerConfig config = {"localhost", 8080, 100, false};
ConfigManager manager(config);
try {
manager.validate();
std::cout << "설정 유효" << std::endl;
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
트러블슈팅
문제 1: struct에 private 멤버
증상: 의도와 다른 접근 제어
// ❌ struct에 private (혼란스러움)
struct Point {
private: // struct인데 private?
int x, y;
public:
Point(int x, int y) : x(x), y(y) {}
int getX() const { return x; }
};
// ✅ class 사용
class Point {
private:
int x, y;
public:
Point(int x, int y) : x(x), y(y) {}
int getX() const { return x; }
};
문제 2: class에 모두 public
증상: 의도와 다른 접근 제어
// ❌ class에 모두 public (혼란스러움)
class Point {
public:
int x, y; // class인데 public?
};
// ✅ struct 사용
struct Point {
int x, y;
};
문제 3: POD 조건 위반
증상: C 호환 불가
// ❌ 비POD (생성자 있음)
struct Point {
int x, y;
Point(int x, int y) : x(x), y(y) {}
};
static_assert(!std::is_pod_v<Point>); // false
// ✅ POD
struct Point2 {
int x, y;
};
static_assert(std::is_pod_v<Point2>); // true
// C 호환
extern "C" {
void processPoint(Point2* p);
}
문제 4: 상속 접근 제어 혼란
증상: 의도와 다른 상속
class Base {
public:
void foo() {}
};
// ❌ struct인데 private 상속 (명시 필요)
struct Derived : private Base { // private 명시
};
Derived d;
// d.foo(); // ❌ 컴파일 에러 (private 상속)
// ✅ struct는 기본 public 상속
struct Derived2 : Base { // public 상속 (기본)
};
Derived2 d2;
d2.foo(); // ✅ OK
마무리
struct와 class의 차이는 기본 접근 제어뿐입니다.
핵심 요약
- struct vs class
- struct: 기본 public
- class: 기본 private
- 기능은 완전히 동일
- 사용 규칙
- 데이터만: struct
- 메서드 + 캡슐화: class
- POD 필요: struct
- POD 타입
- C 호환
- memcpy 가능
- 바이너리 직렬화 가능
- 성능
- struct와 class는 성능 차이 없음
- 캡슐화가 성능에 영향 없음
선택 가이드
| 상황 | 권장 | 이유 |
|---|---|---|
| 데이터만 | struct | 의도 명확 |
| 캡슐화 필요 | class | 불변식 보호 |
| C 호환 | struct (POD) | 바이너리 호환 |
| 메서드 많음 | class | 관례 |
| 간단한 값 타입 | struct | 간결 |
코드 예제 치트시트
// struct: 데이터만
struct Point {
int x, y;
};
// class: 캡슐화
class BankAccount {
private:
double balance_;
public:
BankAccount(double initial) : balance_(initial) {}
void deposit(double amount) { balance_ += amount; }
double getBalance() const { return balance_; }
};
// POD 확인
static_assert(std::is_pod_v<Point>);
// C 호환
extern "C" {
void processPoint(Point* p);
}
다음 단계
- 클래스 기초: C++ 클래스 기초
- 접근 제어: C++ 접근 제어
- POD 타입: C++ POD 타입
참고 자료
- “Effective C++” - Scott Meyers
- “C++ Primer” - Stanley Lippman
- Google C++ Style Guide: https://google.github.io/styleguide/cppguide.html 한 줄 정리: struct는 데이터 컨테이너, class는 캡슐화된 객체로 사용하고, 기능은 완전히 동일하지만 의도를 명확히 표현하는 것이 중요하다.
심화 부록: 구현·운영 관점
이 부록은 앞선 본문에서 다룬 주제(「C++ struct vs class | 접근 제어·POD·C 호환 완벽 비교」)를 구현·런타임·운영 관점에서 다시 압축합니다. 도메인별 세부 구현은 글마다 다르지만, 입력 검증 → 핵심 연산 → 부작용(I/O·네트워크·동시성) → 관측의 흐름으로 장애를 나누면 원인 추적이 빨라집니다.
내부 동작과 핵심 메커니즘
flowchart TD A[입력·요청·이벤트] --> B[파싱·검증·디코딩] B --> C[핵심 연산·상태 전이] C --> D[부작용: I/O·네트워크·동시성] D --> E[결과·관측·저장]
sequenceDiagram participant C as 클라이언트/호출자 participant B as 경계(런타임·게이트웨이·프로세스) participant D as 의존성(API·DB·큐·파일) C->>B: 요청/이벤트 B->>D: 조회·쓰기·RPC D-->>B: 지연·부분 실패·재시도 가능 B-->>C: 응답 또는 오류(코드·상관 ID)
- 불변 조건(Invariant): 버퍼 경계, 프로토콜 상태, 트랜잭션 격리, FD 상한 등 단계별로 문장으로 적어 두면 디버깅 비용이 줄어듭니다.
- 결정성: 순수 층과 시간·네트워크·스케줄에 의존하는 층을 분리해야 테스트와 장애 분석이 쉬워집니다.
- 경계 비용: 직렬화, 인코딩, syscall 횟수, 락 경합, 할당·GC, 캐시 미스를 의심 목록에 둡니다.
- 백프레셔: 생산자가 소비자보다 빠를 때 버퍼·큐·스트림에서 속도를 줄이는 신호를 어디에 둘지 정의합니다.
프로덕션 운영 패턴
| 영역 | 운영 관점 질문 |
|---|---|
| 관측성 | 요청 단위 상관 ID, 에러율·지연 p95/p99, 의존성 타임아웃·재시도가 대시보드에 보이는가 |
| 안전성 | 입력 검증·권한·비밀·감사 로그가 코드 경로마다 일관적인가 |
| 신뢰성 | 재시도는 멱등 연산에만 적용되는가, 서킷 브레이커·백오프·DLQ가 있는가 |
| 성능 | 캐시·배치 크기·커넥션 풀·인덱스·백프레셔가 데이터 규모에 맞는가 |
| 배포 | 롤백 룬북, 카나리/블루그린, 마이그레이션·피처 플래그가 문서화되어 있는가 |
| 용량 | 피크 트래픽·디스크·FD·스레드 풀 상한을 주기적으로 검증하는가 |
스테이징은 데이터 양·네트워크 RTT·동시성을 프로덕션에 가깝게 맞출수록 재현율이 올라갑니다.
확장 예시: 엔드투엔드 미니 시나리오
앞선 본문 주제(「C++ struct vs class | 접근 제어·POD·C 호환 완벽 비교」)를 배포·운영 흐름에 맞춰 옮긴 체크리스트입니다. 도메인에 맞게 단계 이름만 바꿔 적용할 수 있습니다.
- 입력 계약 고정: 스키마·버전·최대 페이로드·타임아웃·에러 코드를 경계에 둔다.
- 핵심 경로 계측: 요청 ID, 단계별 지연, 외부 호출 결과 코드를 로그·메트릭·트레이스에서 한 흐름으로 본다.
- 실패 주입: 의존성 타임아웃·5xx·부분 데이터·락 대기를 스테이징에서 재현한다.
- 호환·롤백: 설정/마이그레이션/클라이언트 버전을 되돌릴 수 있는지 확인한다.
- 부하 후 검증: 피크 대비 p95/p99, 에러율, 리소스 상한, 알림 임계값을 점검한다.
handle(request):
ctx = newCorrelationId()
validated = validateSchema(request)
authorize(validated, ctx)
result = domainCore(validated)
persistOrEmit(result, idempotentKey)
recordMetrics(ctx, latency, outcome)
return result
문제 해결(Troubleshooting)
| 증상 | 가능 원인 | 조치 |
|---|---|---|
| 간헐적 실패 | 레이스, 타임아웃, 외부 의존성, DNS | 최소 재현 스크립트, 분산 트레이스·로그 상관관계, 재시도·서킷 설정 점검 |
| 성능 저하 | N+1, 동기 I/O, 락 경합, 과도한 직렬화, 캐시 미스 | 프로파일러·APM으로 핫스팟 확인 후 한 가지씩 제거 |
| 메모리 증가 | 캐시 무제한, 구독/리스너 누수, 대용량 버퍼, 커넥션 미반납 | 상한·TTL·힙/FD 스냅샷 비교 |
| 빌드·배포만 실패 | 환경 변수, 권한, 플랫폼 차이, lockfile | CI 로그와 로컬 diff, 런타임·이미지 버전 핀 |
| 설정 불일치 | 프로필·시크릿·기본값, 리전 | 스키마 검증된 설정 단일 소스와 배포 매트릭스 표준화 |
| 데이터 불일치 | 비멱등 재시도, 부분 쓰기, 캐시 무효화 누락 | 멱등 키·아웃박스·트랜잭션 경계 재검토 |
권장 순서: (1) 최소 재현 (2) 최근 변경 범위 축소 (3) 환경·의존성 차이 (4) 관측으로 가설 검증 (5) 수정 후 회귀·부하 테스트.
배포 전에는 git add → git commit → git push 후 npm run deploy 순서를 권장합니다.
자주 묻는 질문 (FAQ)
Q. 이 내용을 실무에서 언제 쓰나요?
A. C++ struct vs class 차이는 기본 public/private뿐, 기능은 동일합니다. 데이터 묶음 vs 캡슐화 관례, POD·C 호환까지 문법은 같고 의도 표현과 선택 기준을 비교합니다. Start no… 실무에서는 위 본문의 예제와 선택 가이드를 참고해 적용하면 됩니다.
Q. 선행으로 읽으면 좋은 글은?
A. 각 글 하단의 이전 글 또는 관련 글 링크를 따라가면 순서대로 배울 수 있습니다. C++ 시리즈 목차에서 전체 흐름을 확인할 수 있습니다.
Q. 더 깊이 공부하려면?
A. cppreference와 해당 라이브러리 공식 문서를 참고하세요. 글 말미의 참고 자료 링크도 활용하면 좋습니다.
같이 보면 좋은 글 (내부 링크)
이 주제와 연결되는 다른 글입니다.
- C++ Template Lambda | ‘템플릿 람다’ 가이드
- C++ 컴파일 타임 프로그래밍 | constexpr·consteval·if constexpr 완벽 가이드
- C++ if constexpr | ‘컴파일 타임 if’ 가이드
이 글에서 다루는 키워드 (관련 검색어)
C++, struct, class, 접근제어, POD, 자료구조, 객체지향 등으로 검색하시면 이 글이 도움이 됩니다.