C++ 에서 const 타입과 not const 타입의 오버로딩 .. ㅜㅜ

naddolki의 이미지

C++ 에서 함수 오버로딩할 때 ..

void plusOne(int *);
void plusOne(const int *);

위의 경우 정상적으로 "오버로딩" 이 되는데..

void plusOne(int);
void plusOne(const int);

는 오버로딩이 되질 않을까요 ? (함수 중복 정의 오류가 남)

가령 ..

int plusOne(const int a)
{
return a+1;
}

int plusOne(int a)
{
return ++a;
}

위와 같은 두 경우 모두 사용이 가능해야 하는 게 아닐까요 ?

아시는 분 계시면 .. 좀 알려주셔요 ㅠㅠ

klyx의 이미지

호출할 떄 구분할 수 있는 방법이 없기 때문입니다.
인자를 넘겨줄 때, const int* 는 int*로 캐스팅 없이는 복사될 수 없기 때문에, const int*와 int*는 구분이 가능합니다.

const int *a = ...
int *b = a // error!!!

하지만 int 와 const int는 서로 복사가 가능하므로 함수 호출할 때 어느쪽을 부른 걸지 알수가 없습니다.

const int a = ...
int b = a // no problem

etension의 이미지

void plusOne(const int var);
일반적으로 위와 같이 함수 파라미터에 const 를 쓴다는 것은 해당 파라미터 값을 함수 내에서 변경 시키지 않겠다는
의미란 것은 잘 알고 계시리라 생각합니다.

void plusOne(const int *var);
하지만 위와 같은 경우의 var 변수는 바뀔 수 있습니다. 다른 주소를 포인팅 할 수 있죠.
다시말해 var 변수 자체가 const가 아니라 const int* 라는 타입을 가지는 변수 입니다.

void plusOne(int *);
void plusOne(const int *);

즉 위와 같은 경우는 파라미터로 넘어오는 변수의 타입이 다르기 때문에 함수를 사용할때 넘겨진 인자를 통해
사용될 함수를 구분하고 결정 할 수 있지만

void plusOne(int *);
void plusOne(int * const );

이런 경우는 구분 하지 못합니다. (위 같은 경우는 같은 타입을 넘겨받지만 아래 것은 값을 변경하지 않겠다는 의미입니다.)

댓글 달기

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