c++ mem_fun_ref with operator [] subscript
      글쓴이: catzbi / 작성시간: 일, 2003/10/12 - 12:52오전    
  
  typedef struct hello { 
	bool operator [] ( int k ) { 
		cout << k <<endl; 
		return true ; 
	}
	 bool operator () () {
		cout <<"hello"<<endl; 
		return true ;
	}
}HELLO_t ; 
void main() { 
	malloc_allocator < int > mi; 
	malloc_allocator<int> ::rebind<long>::other  ml; 
	
	tagTest1<long> t1; 
	
	HELLO_t h[10]; 
	for_each ( h , h+ 10 , mem_fun_ref ( &HELLO_t::operator()  ) ); 
	int a = 2 ; 
	for_each ( h , h+ 10 , mem_fun_ref ( &HELLO_t::operator[] ) ); // 이부분...
	cout << typeid ( h[0][1]).name() <<endl ; 
	cout << typeid ( h[0].operator[](1) ) .name() <<endl; 
	
} 
뭐가 빠진듯.. 도와주세요.
Forums: 


-_-; 허걱 무지 어렵게 프로그래밍 하시네요저도 STL 를 좋아하지
-_-; 허걱 무지 어렵게 프로그래밍 하시네요
저도 STL 를 좋아하지만 이건좀...
컴파일 해보니 에러는 여기에서 납니다
bool operator () () { cout <<"hello"<<endl; return true ; }승자는 자기보다 우월한 사람을 보면 존경심을 갖고 그로부터 배울 점을 찾지만 패자는 자기보다 우월한 사람을 만나면 질투심을 갖고 어디 구멍난 곳이 없는지 찾는다.
- 하비스
[code:1]class hello { public
class hello { public: bool operator [] ( hello& tt ) { return true ; } void operator()(hello& tt) { cout <<"hello"<<endl; } }; int main() { // malloc_allocator < int > mi; // malloc_allocator<int> ::rebind<long>::other ml; // tagTest1<long> t1; hello aa; hello h[10]; for_each ( h , h+ 10 ,aa); int a = 2 ; // for_each ( h , h+ 10 , mem_fun_ref ( &hello::operator[] ) ); // 이부분... // cout << typeid ( h[0][1]).name() <<endl ; // cout << typeid ( h[0].operator[](1) ) .name() <<endl; system("pause"); return 0; }시간상 -_-; 이렇게 컴파일 되게 만들었는데요
위부분은 Effective STL 260페이지에 자세히 나옵니다 [해답이 될듯]
책에선 위에 처럼 짜신거[똑같은건 아니고요]에 대해 한마디 나오네요
-_-;;
참고하세요
승자는 자기보다 우월한 사람을 보면 존경심을 갖고 그로부터 배울 점을 찾지만 패자는 자기보다 우월한 사람을 만나면 질투심을 갖고 어디 구멍난 곳이 없는지 찾는다.
- 하비스
[code:1]typedef struct hello {
typedef struct hello { bool operator [] ( int k ) { cout << k <<endl; return true ; } bool operator () () { cout <<"hello"<<endl; return true ; } }HELLO_t ;위 코드를 보면 operator[] 는 int 를 매개변수로 받습니다.
그런데
for_each ( h , h+ 10 , mem_fun_ref ( &HELLO_t::operator[] ) );호출할 때는 매개변수없이 호출하려고 하니 에러가 날 수 밖에 없습니다.
어떤 고정값을 매개변수로 넘겨서 호출(예를 들어 1)하려고 한다면
<functional> 헤더를 인클루드하고
for_each ( h , h+ 10 , bind2nd(mem_fun_ref ( &HELLO_t::operator[] ) , 1));로 바꾸면 될겁니다.
그런데 약간 이상한 것은 bind2nd는 매개변수 2개를 받는 함수의 두번째 매개변수를
고정하는 것인데 여기서는 매개변수를 하나받는 것의 매개변수를 고정하는데
쓰이고 있고 아무문제 없이 잘 동작합니다. 그리고 제가 가지고 있는 책에도
이런식으로 쓰는 예제가 나옵니다. 왜 이게 가능한지 혹시 아시는 분 계신가요???
호출객체 자신이 첫번째 매개변수인 것일까요?
그냥 찍어봤습니다.. ^_^...
소스 결말
void main() { HELLO_t h[10]; for_each ( h , h+ 10 , mem_fun_ref ( &HELLO_t::operator() ) ); int a = 2 ; vector < HELLO_t * > vh ; vh.push_back ( new HELLO_t ) ; vh.push_back ( new HELLO_t ) ; vh.push_back ( new HELLO_t ) ; vh.push_back ( new HELLO_t ) ; vh.push_back ( new HELLO_t ) ; // for_each ( h , h+ 10 , bind2nd ( mem_fun1_ref ( &HELLO_t::operator[] ) , a ) ); // 이부분... for_each ( vh.begin() , vh.end() , bind2nd ( mem_fun1 ( &HELLO_t::operator[] ) , a ) ); cout << typeid ( h[0][1]).name() <<endl ; cout << typeid ( h[0].operator[](1) ) .name() <<endl; }삽질하지 말자.. > -,.=;
어
댓글 달기