CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: ostream

  1. #1
    Join Date
    May 2018
    Posts
    158

    ostream

    With this code:

    Code:
    #include <iostream>
    using  namespace std;
    
    Display::Display(int N)
    {
        (N<1 || N%2==0) ? dim=5 : dim=N;
        matr=new int*[dim];
        for (int i=0; i<dim; i++)
            matr[i]=new int[dim];
        for (int r=0; r<dim; r++)
            for (int c=0; c<dim; c++)
                matr[r][c]=0;
    }
    
    ostream& operator<<(ostream& os, const Display& m)
    {
        for (int r=0; r<m.dim; r++) {
            os << "\n";
            for (int c=0; c<m.dim; c++)
                os << m.matr[r][c];
        }
        return os;
    }
    
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
        Display d(-2);
        cout<<d << endl;
        Display d1(1);
        cout<<d << endl;
        Display d2(4);
        cout<<d << endl;
    }
    I have doubt about this operator<< code, infact I replace it with (I replaced n.2 os with cout):

    Code:
    ostream& operator<<(ostream& os, const Display& m)
    {
        for (int r=0; r<m.dim; r++) {
            cout << "\n";
            for (int c=0; c<m.dim; c++)
                cout << m.matr[r][c];
        }
        return os;
    }
    compiler gives no errors, when exe runs gives right results but I don't understand it's not the same thing!
    Last edited by 2kaud; May 24th, 2018 at 11:02 AM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: ostream

    [When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'].

    In the replacement code (using cout), this will give the same result when stream insertion (<<) is used with the cout stream as in that case os is cout. However, if you want to use stream insertion into a different stream (say for a file), then the replacement code won't do as expected as the insertion will always be to the cout stream and not to the file stream.

    consider
    Code:
    ofstream ofs("test.txt");
    ...
    ofs << d << endl;
    For the original code, this will work as expected and the contents of d will be written to the file. However, with the replacement code the contents will still be written to cout and nothing will be written to the file.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured