코딩/C++

[c++] using namespace 사용하기

peter_00 2024. 8. 19. 17:39
반응형

std 는 옵션이다.

 

using namespace std;

using std::cout;

using namespace std;

를 사용하면, std를 제외하고 코딩을 할 수 있다. 

 

예를 들어 

 

// Below, must be coded in 1 file

// e.g Example.h

class Exmaple 

{

 

} // end of class declaration

 

namespace 는 많은 파일에서도 공용으로 사용 가능하다.

using 이라는 함수를 이용하면 namespace 를 scope 으로 부를 수 있다.

#include <iostream>
using namespace std;

namespace Ns
{
	int i;
}
// 갭이 있음
{
	int j;
}

int main ()
{
	NS::i = NS::j = 10;
    cout << NS::i * NS::j << endl;
    
    using namespace NS;
    cout << i*j << endl;
    return 0;
}

using namespace std; 를 했기 때문에 cout에 원래의 형식인 std::cout 를 사용하지 않아고 바로 cout을 사용해도됨

 

오브젝트 i 와 j 가 NS에 귀속 

NS:: 을 반복해서 사용하지 않기 위해서 using namespace NS 를 사용

 

그 결과 단순히 i*j 를 하면 10 * 10 이므로 결과값은 100이됨

 

헤더 파일과 .cpp 파일의 관계 예시

#ifndef _COUNTER_H_
#define _COUNTER_H_
// 헤더 파일 가이드
int upperbound;
int lowerbound;
// 2개의 글로벌 variable
class counter {
    public:
    	// constructor
       	counter(int n) {
                if(n<=upperbound) count = n;
                else count = upperbound;
        }
        // reset method
        void reset(int n){
                if(n<=upperbound) count=n;
        }
        // run method
        int run() {
                if(count > lowerbound) return count--;
                else return lowerbound;
        }
    private:
        int count;
};
#endif
// .cpp 파일
#include <iostream>
#include "counter.h"
using namespace std;

int main()
{
    // 헤더 파일에 variable들이 있기 때문에 엑세스 가능
    upperbound = 5000;
    lowerbound = 1;
    counter ob1(10);

    int i=0;
    cout<<"Counter Object:";
    do {
        i = ob1.run();
        cout<<i<<" ";
    } while (i>lowerbound);
    cout<<endl;
}
#ifndef _DELAY_H_
#define _DELAY_H_
int upperbound;
int lowerbound;
class delay {
   private:
       int count;

   public:
        delay(int n) {
                if(n<=upperbound) count = n;
                else count = upperbound;
        }
        void reset(int n){
                if(n<=upperbound) count=n;
        }
        int run() {
                if(count > lowerbound) return count--;
                else return lowerbound;
        }
};
#endif

위와 같이 delay.h와 counter.h를 하나의 .cpp 파일에 #include 를 할 경우 에러가 발생한다 두개의 헤더 파일에서 같은 이름의 함수를 적용했기 때문이다. 이름이 같으면 에러가 발생한다.

그림으로 설명

위와 같은 에러를 방지하기 위해서 delay.h 헤더파일에 전용 namespace를 만들어주면 된다. 

#ifndef _DELAY_H_
#define _DELAY_H_
namespace NS_Delay {
int upperbound;
int lowerbound;
class delay {
    public:
        delay(int n) {
                if(n<=upperbound) count = n;
                else count = upperbound;
        }
        void reset(int n){
                if(n<=upperbound) count=n;
        }
        int run() {
                if(count > lowerbound) return count--;
                else return lowerbound;
        }
    private:
 	int count;
};
}
#endif

namespace NS_Delay 의 { 브라켓 안에 있기 때문에 그 안에 코드를 넣으면 같은 이름의 함수여도 에러가 없을것이다.

가장 이상적인 코드 형식

위와 같은 형식으로 namespace 를 구분 해두면 name clash 는 발생하지 않을것이다.

namespace 를 전체의 코드에 적용하고 싶다면 main 코드가 있는 .cpp 파일에 using namespace std; 를 하는게 좋다, 그렇게 해야 전체 적용이 가능하기 때문이다.

 

Namespace 단축

namespace Very_very_long_varible_name
{
	int example();
}

namespace VVLN = Very_very_long_varible_name;

Very_very_long_varible_name을 VVLN 으로 줄였다.

 

Nested namespace 에도 namespace 단축을 이용 가능하다

namespace University_of_example 
{
	int student();
	namespace Nest_STUDENT; 
  {
		void a() { j++; }
		int j;
		void b() { j++; } 
	}
}
namespace STUDENT = University_of_example::Nest_STUDENT;

 

University_of_example 안에 있는 Nest_STUDENT에 속해있는 함수 3개에 엑세스 하기 위해서 STUDENT라는 새로운namespace 를 만들어 엑세스 가능하게 만들 수 있다.

반응형