0

I'am currently trying to filter input streamed to the 'clog' streambuffer. Therefore i wanted to create a class 'MyClog' which overloads it's << operator and filters the input streamed to it before forwarding the filtered input to the clog streambuffer.

The filtering depends on an internal state of the MyClog class. Also i want to overload the operator using a template, so i just have to implement one method.

example

clog << "This is a Test No." << 1;

myclog << "This is a Test No." << 2

Depending of the internal state of myclog the message is printed out via clog.

My overloaded operator works if i'am not using a template, what am i doing wrong? And what is the difference between template<typename T> and template<class T>?

Here is an excerpt of my code.

template<typename T>
class MyClog {

private:
bool state=false;

public:
MyClog& operator<<(const T& arg){
    if(state){
        clog << arg;
    }
    return *this;
}
peetzweg
  • 63
  • 4

1 Answers1

0

template<typename T> is preferred over template<class T>, but they are equivalent.

You are templating the entire class when you only need to template one function - this should work:

class MyClog {

private:
bool state=false;

public:

template<typename T>
MyClog& operator<<(const T& arg){
    if(state){
        clog << arg;
    }
    return *this;
}
JHood
  • 16