vector assign 할때

jinserk의 이미지

vector 에 assign 하는 것을 좀더 편하게 하기 위해서 아래와 같은 class 를 만들었습니다.

template <typename T>
class myvector
{
    private:
        std::vector<T> data_;
 
    public:
        myvector() {};
        myvector(unsigned size) {
            data_.resize(size);
        }
 
        inline myvector<T>& operator=(const myvector<T>& rhs) {
            data = rhs.data_;
            return *this;
        }
 
        inline myvector<T>& operator=(const T& rhs) {
            data_.clear();
            data_.push_back(rhs);
            return *this;
        }
 
        inline myvector<T>& operator,(const T& rhs) {
            data_.push_back(rhs);
            return *this;
        }
 
        inline T& operator[](const unsigned i) {
            return data_[i];
        }
 
        inline unsigned size(void) {
            return data_.size();
        }
};

이런 클래스를 정의하고 나서 다음과 같이 입력을 해봤습니다.

myvector<char> x = 1, 2, 3, 4, 5;
 
for (int i = 0; i < x.size() < i++)
   cout << x[i] << "  " << endl;

원래 기대했던 결과는 1 2 3 4 5 가 나오는 건데,
이상한 문자들이 찍혀 나옵니다.

열씸히 들여다보아도 뭐가 잘못된건지 잘 모르겠네요.
도움 부탁드립니다.

감사합니다.

danskesb의 이미지

http://www.parashift.com/c++-faq-lite/operator-overloading.html wrote:

Avoid overloading the comma operator: x, y. The overloaded comma operator does not have the same ordering properties that it has when it is not overloaded, and that confuses users.

---- 절취선 ----
http://blog.peremen.name

jinserk의 이미지

바보같이 cout << (int)x[i] 한다는 걸 빼먹었네요.

그래도 혹시라도 고수분들께서 위 코드에 이상한점을 지적해주시리라 믿고
걍 놔둘까 합니다. ^^;

일종의 stl vector 의 wrapper 클래스입니다.
입출력과 vector + - * / 를 편하게 하려는 목적으로 만들었습니다.

Leo.

danskesb의 이미지

저 위에서 사용한
 myvector x=1, 2, 3, 4, 5;
여기에 사용된 , 연산자는 myvector::operator ,가 호출된 걸까요? 아니면 C++의 원래 의미로 사용된 걸까요? 아니면 생성자에 인수로 넘겨진 걸까요? 명확한 답을 못 내리겠다면 컴퓨터도 못 내립니다.

찾아 보니까 Blitz++ 라이브러리에서 어떻게 ,를 재정의했는지에 대한 문서가 있긴 있네요.

http://c2.com/cgi/wiki?OverloadingCommaOperator

---- 절취선 ----
http://blog.peremen.name

jinserk의 이미지

코드를 올릴때

[code} ... [/code] 사이에 코드를 넣었더니
myvector<char> x = 1, 2, 3, 4, 5; 에서 <char> 가 나타나질 않는군요.
다시 <code> ... 로 수정해서 올렸습니다.

저런 경우는 myvector::operator, 호출이 확실한 거겠죠? ^.^

Leo.

lifthrasiir의 이미지

그냥 대입문인 x = 1, 2, 3, 4, 5;면 모르겠으나 저 경우는 애초에 컴파일조차 되지 않습니다. 변수 선언에서의 ,는 변수를 구분하지 operator,를 절대로 호출하지 않습니다. (그렇다고 (1, 2, 3, 4, 5)라고 해도 호출될 리는 없고, 아무래도 (myvector<char>(), 1, 2, 3, 4, 5) 식으로 써서 copy constructor를 사용하도록 하는 거 말고 다른 방법은 없어 보입니다.)

댓글 달기

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