#include <iostream>
int main()
{
using namespace std;
int apple = 5;
cout << apple << endl;
{
cout << apple << endl;
int apple = 1;
cout << apple << endl;
}
cout << apple << endl;
return 0;
}
5
5
1
5
#include <iostream>
using namespace std;
int value = 123;
int main()
{
cout << value << endl;
int value = 1;
cout << ::value << endl;
cout << value << endl;
return 0;
}
123
123
1
static int a
가 선언이 될 때 변수 a
의 메모리 주소가 고정된다는 뜻이다.#include <iostream>
using namespace std;
void doSomething()
{
🔴int a = 1;
🔵static int a = 1;
++a;
cout << a << endl;
}
int main()
{
doSomething();
doSomething();
doSomething();
doSomething();
doSomething();
doSomething();
return 0;
}
🔴 일 때
2
2
2
2🔵 일 때
2
3
4
5"디버깅 해보면 static int a 는 처음에만 초기화 된다."
#include <iostream>
using namespace std;
// forward declaration
/*extern*/ void doSomething();
extern int a;
int main()
{
doSomething();
doSomething();
cout << a << endl;
return 0;
}
#include <iostream>
extern int a = 123;
void doSomething()
{
using namespace std;
cout << "hello " << endl;
}
- 아무것도 없이 전방선언을 해두면 컴파일러는 다른 cpp파일에서 선언 했겠구나 하고 찾는다
- 다른 cpp파일에서 전방선언한 (위에 예시를 든) 함수를 발견하면 linking 한다.
extern
은 생략가능
**
Internal linkage
static int g_x
;const int g_x(1)
;external linkage
int g_x
;extern int g_x(1)
;#include <iostream>
#include "MyConstants.h"
void doSomething();
int main()
{
using namespace std;
cout << "In main.cpp file : \t" << Constants::pi << " "
<< &Constants::pi << endl;
doSomething();
return 0;
}
test.cpp
#include<iostream>
#include"MyConstants.h"
void doSomething()
{
using namespace std;
cout << "In test.app : \t\t" << Constants::pi << " "
<< &Constants::pi << endl;
}
MyConstants.h
#pragma once
namespace Constants
{
const double pi(3.141592);
const double gravity(9.8);
// ...
}
In main.cpp file : 3.14159 00699B30
In test.app : 3.14159 00699B70같은 전역 변수를 사용했는데 메모리 위치가 다르다.
=> 각자 따로따로 메모리를 사용한다.
=> 많이 사용하게 된다면 메모리를 엄청 많이 차지할 것 이다.
=> 헤더파일에서는 전역 상수 이름만 선언, cpp파일을 만들어서 상수 초기화, 초기화 한 변수를 사용하면 하나의 메모리로 모두 표현 가능하다.
#pragma once
namespace Constants
{
extern const double pi;
extern const double gravity;
// ...
}
MyConstants.cpp
namespace Constants
{
extern const double pi(3.141592);
extern const double gravity(9.8);
}
In main.cpp file : 3.14159 00229B68
In test.app : 3.14159 00229B68참조하는 메모리주소가 같음을 확인할 수 있다.
auto add(int x, int y) -> int
{
return x + (double)y;
}
int main()
{
auto a = 123; // 형 추론
auto d = 123.0;
auto c = 1 + 2.0;
auto result = add(1, 2);
return 0;
}
float a = 1.0f;
=> double d = a;
double d = 3;
, short s = 2;
int a = 30000;
char c = i;
// c는 문자형이라 숫자로 안나옴. 결과 값 확인을 위해 static casting 함
cout << static_cast<int>(c) << endl;
48
cout << 5u - 10 ;
=> 이상한 값 나옴static_cast<int>(c)
int c = (int) 4
int c = int(4)
꿀팁 : 아래 코드는 데이터 타입을 리턴해준다
#include <typeinfo>
cout << typeid(variable or data).name() << endl;
#include <iostream>
#include <typeinfo>
enum Color // user-defined data types
{
COLOR_BLACK,
COLOR_RED,
COLOR_BLUE,
COLOR_GREEN,
COLOR_ORANGE,
// ...
};
enum Feeling
{
HAPPY = -3 ,
JOY,
TIRED,
BLUE,
};
int main()
{
using namespace std;
Color paint = COLOR_BLACK;
Color house(COLOR_BLUE);
Color appe{ COLOR_RED };
Color my_color = COLOR_BLACK;
Feeling my_feeling = TIRED;
cout << my_color << " " << COLOR_RED << endl;
cout << my_feeling << " " << HAPPY << endl;
return 0;
}
enum class Color
{
RED,
BLUE,
};
enum class Fruit
{
BANANA,
APPLE,
};
int main()
{
using namespace std;
Color color = Color::RED;
Fruit fruit = Fruit::BANANA;
// RED와 BANANA는 둘다 0 이지만 다른 것으로 인식 한다.
return 0;
}
#include <iostream>
#include <string>
using namespace std;
struct Person
{
double height;
float weight;
int age;
string name = "NONE";
void print()
{
cout << height << " " << weight << " " << age << " " << name;
cout << endl;
}
};
Person getMe()
{
Person me{ 2.0, 100.0, 20, "Jack" };
return me;
}
int main()
{
Person a;
cout << a.name << endl;
Person me{ 2.0, 100.0, 20, "Jack" };
Person me2(me);
Person me3;
me3 = me;
me.print();
me2.print();
me3.print();
return 0;
}
NONE
2 100 20 Jack
2 100 20 Jack
2 100 20 Jack
struct Employee // 2 + 4 + 8 = 14
{
short id; // 2 bytes
int age; // 4 bytes
double wage; // 8 bytes
};
int main()
{
Employee emp1;
cout << sizeof(Employee) << endl;
return 0;
}
16