[Basic]포인터,네임스페이스,헤더 /Pointer,Namespace,Header

👉🏻 포인터 / Pointer

✔️ 변수의 주소 값을 저장하는 변수
A variable that stores the address value of a variable

✔️ 주소연산자(&)를 사용하면 변수의 주소를 가져올 수 있습니다.
You can get the address of a variable by using the address operator (&).

string* ptv = &name;

👉🏻네임스페이스 / namespace

✔️여러 라이브러리나 모듈을 함께 사용할 때, 해당 이름들을 그룹화하여 서로 간섭하지 않도록 격리하는 역할을 합니다.
When using multiple libraries or modules together, it serves to group their names and isolate them to prevent interference.

✔️ cout을 사용하는 경우 아래에서 두가지 형태의 코드를 볼 수 있습니다.
When using cout, you can see two forms of code below.

✔️ usiing namespace std; 를 사용하면 “std::” 를 생략 할 수 있습니다.
If you use namespace std; you can omit “std::”.

— 첫번째 / first

cout << name << "\n";

–두번째 / second

std::cout << "ftest message\n" << std::endl;

👉🏻헤더 / header

✔️ 일반적으로 헤더파일(.h)에는 책의 목차처럼 기능에 대한 목록을 정의합니다.
Generally, header files (.h) define a list of functions, much like a table of contents in a book.

✔️ cpp파일에 실질적은 기능을 정의합니다.
Define the actual functionality in the cpp file.

✔️ 헤더파일에 기능까지 정의할 수 있지만 특별한 경우가 아니면 권장되지 않습니다.
You can define functions in header files, but this is not recommended unless there is a special case.

👉🏻코드 / Code

✔️ header.h

// if not define HEADER_H
// -- 만약 HEADER_H심볼이 정의되어 있지 않으면
//    If the HEADER_H symbol is not defined
// define HEADER_H
// -- HEADER_H 심볼을 정의한다.
//    Defines the HEADER_H symbol.
//
#ifndef HEADER_H
#define HEADER_H


// 네임스페이스 / namespace
namespace my {

    // -- 클래스를 사용할 경우 -- /
    // -- When using classes -- /
    //
    // class Server {
    //     public:
    //     int add(int x, int y)
    //     {
    //         return x + y;
    //     }
    // };


    // -- 멤버 함수로 사용할 경우 -- /
    // -- When used as a member function -- /
    int add(int x, int y)
    {
        return x + y;
    }

}

// 헤더 사용에 권장되지 않은 방식
// Not recommended method for using headers
void ftest(){
    std::cout << "ftest message\n" << std::endl;
}

// 헤더 사용에 권장되는 방식
// Recommended way to use headers
void message (std::string& msg);

#endif

✔️ pointer.cpp

#include <iostream>
#include <string>
#include "header.h" // 헤더 포함 / Includes header

using namespace std; // std 네임스페이스 사용하기 / Using the std namespace

void message(string& msg){
    cout << msg << "\n" << endl;
}

std::string my_message = "pointer complete!!";
int main(){

    // -- 헤더에 클래로 선언된 경우 사용 형식 -- //
    // -- Usage format when declared as a class in the header -- //
    // <네임스페이스>::<클래스네임> <객체>
    // my::Server svr;
    //

    // 일반 string변수 / General string variable
	string name = "Ayloy";

	// *ptr => 포인터변수 / Pointer Variable
	// &food => 일반변수의 주소 값 / Address value of a regular variable
	string* ptv = &name;

	// 변수 출력 /. variable output
	cout << name << "\n";
	cout << &name << "\n";
	cout << ptv << "\n";

	// print value
	cout << *ptv << "\n";

	// 포인터변수에 값 변경
	// change pointer value
	*ptv = "SilverHand";
	cout << *ptv << "\n";

	// 헤더에 클래스로선언된 경우 / When declared as a class in the header
    //cout << "The sum of 1 and 2 is " << svr.add(1, 2) << std::endl;

    // 헤더에 멤버 함수로 선언된 경우 / When declared as a member function in the header
    cout << "The sum of 1 and 2 is " << my::add(1, 2) << std::endl;

    // ftest함수 실행 / Excute ftewt function
    ftest();
    // message함수 실행, 참조로 전달 / Execute message function, passed by reference
    message(my_message);
	return 0;
}

✔️ 컴파일 / Compiling

g++ pointer.cpp -o pointer -std=c++17

✔️실행 / Run

./pointer

✔️ 결과 / Results

Ayloy
0x16cf4f0e0
0x16cf4f0e0
Ayloy
SilverHand
The sum of 1 and 2 is 3
ftest message

pointer complete!!

Leave a Reply