<< operator overloading을 통해 파일로 출력하고 싶은데 어떻게 해야 할까요?

kknd345의 이미지

#include

using namespace std;

class CResultTextOut
{
private:
ofstream out;

public:
CResultTextOut(void);
~CResultTextOut(void);
public:
void operator << (ostream &output);
};

CResultTextOut::CResultTextOut(void)
{
out.open("output/out.txt");
}

CResultTextOut::~CResultTextOut(void)
{
}

void CResultTextOut::operator << (ostream &output)
{
out << output;
}

int main()
{
CResultTextOut out;
out << "Good" << "\n";
}

Operator Overloading을 잘 쓸줄 몰라 어렵네요 ..
대충 짜봤는데 역시나 안 돌아가는군요 ㅜㅠ...

최종적으로 원하는것은 Good이 operator overloading을 통해서 CResultTextOut안에

멤버 변수로 들어 있는 ofstream out인 "output/out.txt"에 출력되게 하는 것입니다.

수정 좀 부탁드립니다.

좋은 하루 되세요



aoziczero의 이미지

class operator_test
{
private:

public:
operator_test(){}
~operator_test(){}
operator_test& operator<<( const std::string& str )
{
std::cout << str;
return *this;
}
};

class operator_test2
{
private:

public:
operator_test2(){}
~operator_test2(){}
void print( const std::string& str )
{
std::cout << str;
}
};

operator_test2& operator<<( operator_test2& test , const std::string& str )
{
test.print(str);
return test;
}

int _tmain(int argc, _TCHAR* argv[])
{
operator_test test;
test << "good" << "\n";

operator_test2 test2;
test2 << "good" << "\n";
getchar();
return 0;
}

두가지 방법 중에 골라 쓰시면 됍니다.~

klyx의 이미지

구체적인 구현은 aoziczero님께서 자세히 적어주셨으니 생략하고, 한가지말씀드리자면, 연산자를 오버로딩할 때는 충분히 심사숙고한후에 작성해야합니다.
특히, 오버로딩시에 연산자의 동작방식을 변경하는 것은 가능하면 피하는게 좋습니다.
적으신 << 연산자의 오버로딩은 반환형식이 없습니다(void)만, 이것은 본래의 << 연산자의 동작과는 전혀 다릅니다.
iostream 클래스들의 <<이나 >> 연산자 오버로딩에 대한 원형만 한번 살표보시더라도 금방 알수 있을 것입니다.

댓글 달기

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