연산자 오버로딩 관련 질문입니다.

Era의 이미지

#include<iostream>
#include<string>
 
using namespace std;
 
class Person{
private:
	string name;
public:
	Person(){ name = "Bob";}
	Person(string name){this->name = name;}
	//Person operator + (const Person &other){return Person(name + "&" + other.name);}
	Person operator + (const Person &other){Person(name + "&" + other.name);}
	void showName(){ cout << "이름 : " << name << endl;}
};
 
 
int main(void){
	Person person1;
	person1.showName();
	Person person2("Alex");
	person2.showName();
	Person result = person1 + person2; //연산자 오버로딩에서 사용한 연산자를 이용해서 정의한다. nb1 + nb2 = nb1.operator+(nb2)
	result.showName();
	return 0;
}

위코드에서

Person operator + (const Person &other){Person(name + "&" + other.name);}

에 return을 붙이지 않았는데 이때 결과가 Alex로 출력되었습니다.
return없이 연산자 오버로딩으로 생성자를 정의한 경우에, nb1의 결과가 반영되지 않는 과정이 궁금합니다.

익명 사용자의 이미지

짜잔. Undefined behavior 당첨입니다.

함수 Person::operator+는 Person을 반환해야 하는데, 그러지 않고 flowing off 되었을 경우 Undefined behavior가 발생합니다.

Undefined behavior가 발생한 프로그램에서는 신비한 과정을 통해 신비한 일이 일어날 수 있습니다.

그런 프로그램을 만들지 마세요.

jshackist의 이미지

그럼 말씀하신 Undefined behavior가 발생하면, 일정한 결과가 아닌 예측 불가능한 결과값만 나오게 되는 것인가요?

익명 사용자의 이미지

예측 불가능한 결과가 일정하게 나올 수도 있고요.

현실적인 관점에서 볼 때, Undefined behavior (이하 UB)가 발생했다고 해서 컴파일러나 실행 환경이 부조리할 정도로 변덕스럽게 동작하도록 만들어지지는 않습니다. 대개 그런 경우를 신경쓰지 않고 만들어질 뿐이죠.

그러니 어떤 결과가 나왔다면 왜 그 결과가 나왔는지 거꾸로 추적해서 분석해 볼 수 있기는 합니다.

하지만 C++언어를 사용하는 입장에서의 가이드라인은 분명합니다. UB를 일으키지 마세요.

나빌레라의 이미지

예측 불가능이라기보다는 사용하는 컴파일러마다 결과가 다릅니다.
Undefined behavior라는게 스팩에는 정의가 없으니 컴파일러 만드는 사람이 알아서 만드는 것이거든요.

----------------------
얇은 사 하이얀 고깔은 고이 접어서 나빌레라

댓글 달기

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