开发者

c++ manipulator

i have a class:

template<class T>
class matrix
{
private:
    int COLS,ROWS;
public:
    inline matrix(int r,int c){
        this->COLS=r;
        this->ROWS=c;
    }

    template<class T2>
    friend ostream& info(ostream& os);
};

i tried in so many ways to implement the info function.but none is succeed.

i want to use it in main function

Matrix<int> M(10,20);

cout<<info<<M;

i want to out put the Matrix class's cols, and rows information.

i have tried so many time in implementing the friend class info, but failed.

any one can tell me how can do it?

sorry, i forget to put the << overloading part.

template<class T2>
friend ostream& operator<< (ostream &out,matrix<T2> &cMatrix);

the implementation:

template<class T2>
ostream & operator<<(ostream &out, matrix<T2> &cMatrix) {
    out<<cMatrix.getCOLS();// sorry for didn't put the get function, it's not easy to put code line by lin开发者_Go百科e here.
    out<<cMatrix.getROWS();
    return out;
}

my << operate works fun.

but when i want to use info, i got errors.

i am not sure, how to implement the own type manipulators as a friend function. i google some, but they are not friend function. and also, it is a kind of template function.

here is what i am tring:

template<class T2>
ostream& info(ostream& os,matrix<T2> &cMatrix)
{
    int cols=cMatrix.getCOLS();
    int rows=cMatrix.getROWS();
    os<<rols<<"X"<<rows<<"  matrix "<<endl;
    return os;
}


If you want to emulate the manipulators like cout << setw(8) << …, there's no standard facility for that and it can't be done with just a function.

The manipulators that take arguments are factory functions that create special functors. The returned object remembers the parameter to the manipulator and implements operator<< to use it.

There are two separate approaches in the example code.

cout<<info<<M; // info changes cout somehow?

and

ostream& info(ostream& os,matrix<T2> &cMatrix) // info behaves like operator<<?

The first approach, adding state to cout, is possible but way too complicated for this.

The second approach begs the question why you don't just call it operator<< and be done.

Alternately, you can call the second directly: info(cout << "hello", M) << "world";

If you're looking for the syntax cout << info(M), which is more like a manipulator, you need something like

struct print_info {
    Matrix &m;
    print_info( Matrix &in ) : m(in) {}
    friend ostream &operator<<( ostream &str, print_info const &pi ) {
         str << pi.m.getcols();
         …
    }
};

print_info info( Matrix &in )
    { return print_info( in ); }

Strictly the function is unnecessary, but it's better not give a class with such a specific function something such a general name as info.

But then again, all of this is unnecessary. Just use the << override you have, which works.


One possible approach if you don't need it to be sticky:

class InfoPrinter {
    std::ostream& os_;
public:
    InfoPrinter(std::ostream& os) : os_(os) {}
    template<class T> std::ostream& operator<<(const Matrix<T>& m) {
        return os_ << m.getCols() << ", " << m.getRows();
    }
};

struct Info {} info;

InfoPrinter operator<<(std::ostream& os, const Info&) {
    return InfoPrinter(os);
}

Usage would be e.g.:

std::cout << info << Matrix<int>(2,3);


I believe it could work by just creating ostream& operator<<(ostream& os); as a member of your Matrix class. Then you can go cout << M << endl;


You should be trying to overload the output operator. Click here for info on how to do it. If you post your updated code, I'll help you some more if necessary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜