c++ 초보질문;; list 에 넣은 객체 함수 접근..

jenix의 이미지

안녕하세요..

이제 막 c++ 을 배우고 있습니다..

std::list< cPoint >points;
std::list< cPoint >::const_iterator p_iterator;

위와 같이 points 로 리스트 컨테이너를 만들었는데요.

객체는 push_back 으로 다 잘 집어넣었는데..

p_iterator._MyNode 에서 어떻게 원래 객체로 접근하는지 모르겠습니다.

vector 같은경우는 ._MyPtr 로 접근을 했었는데요..

list 는 당연히 될꺼같은데 잘 모르겠습니다.

도움좀.. ㅠㅠ

doldori의 이미지

vector에서는 _MyPtr이라는 인터페이스가 없습니다. 아마 구현에 관련된
private 멤버인 듯한데 어떻게 접근했는지 궁금하군요.
STL의 컨테이너에서 제공하는 iterator는 같은 인터페이스를 갖고 있으므로
vector든 list든 같은 방법을 쓰면 됩니다.

list<cPoint> points;
list<cPoint>::iterator it = points.begin();
cPoint& rp = *it;
cPoint* pp = &*it;
jenix의 이미지

앗;;; 우선 답변에 감사드리며 :D

cPoint rp = *it;

로 해야.. 되는데요?;
doldori 님이 가르쳐주신 예는 잘 안되네요 :oops:
vs.net 2003 입니다만;;

그리고 _MyPtr 은.. vs.net 2003 에서 자동완성에 보이길래 :oops: 눌러서 해봤더니.. 객체를 가르키는 포인터더라구요.

음.. 아고 복잡하네용 :twisted:

---------------------------------------------------------------------------
http://jinhyung.org -- 방문해 보세요!! Jenix 의 블로그입니다! :D

myueho의 이미지

cPoint &rp =- *it; 가 맞습니다.
이것은 *it 를 참조합니다.
cPoint rp = *it; 라고 쓰면 *it 를 복사합니다.

맨 앞에 _가 들어간 넘들은 되도록 쓰지 않는 것이 좋습니다. - 시스템에서 사용하는 식별자일 가능성이 높습니다.
왜 _MyPtr이 public인지 모르겠네요.

저 예제가 안된다면.. 혹시 const list 인가요?

일단 좋은 책을 보시는 것이 좋을 것 같습니다. :wink:

doldori의 이미지

참조형을 쓰면 컴파일이 안된다는 뜻인가요? 그럴 리가... :shock:
아, 물론 참조형 변수가 참조하는 리스트의 개체를 지우거나 해서 무효화된
경우에는 그 참조형 변수를 통해 접근하면 안됩니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.