C++ 책보며 독학도중 질문 올립니다

ewqqwe의 이미지

C++을 혼자 독학중인데
C를 완전히 하고 넘어온게 아니라 C 관련부분에서 좀 많이 막히네요

아래와 같은 코드에서 point& pntadder(const point &p1, const point &p2); 이부분인 해석인 안되는데 혹시 어떤의미를 가지는지를 모르겠네요

typedef struct xypoint{
int xpos;
int ypos;
}point;

point& pntadder(const point &p1, const point &p2);

klara의 이미지

C랑 상관없는 부분입니다. 읽고 계신 책의 레퍼런스(참조) 변수에 대한 부분을 찾아보세요.

ewqqwe의 이미지

구조체와는 전혀 상관이 없는 부분인가요?
point& pntadder(const point &p1, const point &p2);의 point 라는 부분이 이해가 안가서요

klara의 이미지

구조체가 들어가서 달라지는 부분은 없습니다.
int& pntadder(const int &p1, const int &p2);
이렇게 써있으면 이해가 되시나요?

ewqqwe의 이미지

그렇게 말씀해주시니 바로 이해가가네요 그런데 왜 point를 앞에 써준건가요? 그냥 int 라고 사용해도 차이는 없는데;;

익명 사용자의 이미지

자료형으로 구조체 point를 사용하기 때문입니다...

구조체 선언 시 typedef를 사용해

typedef struct 구조체명 {

} 구조체 별칭;

와 같은 형태로 선언하는 경우가 많은데요

님의 코드에서는 구조체 별칭에 'point'라는 별칭을 사용하고 있네요..

ewqqwe의 이미지

일일이 친절한 설명 감사합니다
좋을 하루 되세요~

klara의 이미지

제가 적은건 비슷한 모습을 한 point 대신 int를 인자로 받고 돌려주는 함수의 원형일 뿐입니다.
실제로는 point형의 인자를 받고 point형의 참조를 돌려주는 함수죠.
참고로 typedef 구문은 C에서 구조체 이름이 타입명이 아닌 태그로 취급되기 때문입니다.
C++에서는 struct point 라고 적으면 타입명이 point가 될 수 있지만, C에서는 'struct point'가 타입명이 됩니다.
그래서 struct xypoint라고 구조체 타입을 선언한 후에, typedef struct xypoint point 라고 새로운 타입이름을 정의해주고 있는 부분은 C와의 호환성을 고려하지 않는다면 불필요하다고 할 수 있습니다.
순수히 C++을 위해서라면

struct point {
   int xpos;
   int ypos;
};

이걸로 충분합니다.

댓글 달기

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