c++ mem_fun_ref with operator [] subscript

catzbi의 이미지

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; 
	
} 

뭐가 빠진듯.. 도와주세요.

mastercho의 이미지

-_-; 허걱 무지 어렵게 프로그래밍 하시네요
저도 STL 를 좋아하지만 이건좀...

컴파일 해보니 에러는 여기에서 납니다

 bool operator () () { 
      cout <<"hello"<<endl; 
      return true ; 
   } 

승자는 자기보다 우월한 사람을 보면 존경심을 갖고 그로부터 배울 점을 찾지만 패자는 자기보다 우월한 사람을 만나면 질투심을 갖고 어디 구멍난 곳이 없는지 찾는다.
- 하비스

mastercho의 이미지

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페이지에 자세히 나옵니다 [해답이 될듯]

책에선 위에 처럼 짜신거[똑같은건 아니고요]에 대해 한마디 나오네요

Quote:
컴파일은 어림도 없습니다.. ;;

-_-;;

참고하세요

승자는 자기보다 우월한 사람을 보면 존경심을 갖고 그로부터 배울 점을 찾지만 패자는 자기보다 우월한 사람을 만나면 질투심을 갖고 어디 구멍난 곳이 없는지 찾는다.
- 하비스

exsider의 이미지

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개를 받는 함수의 두번째 매개변수를
고정하는 것인데 여기서는 매개변수를 하나받는 것의 매개변수를 고정하는데
쓰이고 있고 아무문제 없이 잘 동작합니다. 그리고 제가 가지고 있는 책에도
이런식으로 쓰는 예제가 나옵니다. 왜 이게 가능한지 혹시 아시는 분 계신가요???

winner의 이미지

그냥 찍어봤습니다.. ^_^...

catzbi의 이미지

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; 
 
} 

삽질하지 말자.. > -,.=;

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.