How to concatenate << operator on an instance of MY_object

I have a class named W_debug. I want to use it at my_oher_class.

I declare it at my_other_class.h
W_debug w_debug;
and / or ( To try if it runs...)
W_debug *w_debug;

But the line :
w_debug<<"hello"<<"how areyou";
Does not work, compiler gives me the error :

passing 'const W_debug' as 'this' argument of 'W_debug& W_debug::operator<<(const char*)' discards qualifiers

This is my code :
(It works fine if I use it using W_debug()<< , that is to say : without create the instance)

1
2
3
4
5
6
7
8
9
10
11
class W_debug
{
public:
    W_debug();
    ~W_debug();
   
  W_debug & operator + (int   data);
  W_debug & operator + (const char data);
  W_debug & operator + (const char * data);
  W_debug & operator + (double data);
.....


And :

1
2
3
4
5
6
7
8
9
W_debug::W_debug() {    }
W_debug::~W_debug(){    }

W_debug& W_debug::operator << (const char * data) {    
    os<<data;
    return *this;
......    

}


Any help ? Thanks
Last edited on
Looks like the w_debug object is const. Since operator<< is not, you can't call w_debug << "..."
And .... How can I made w_debug not const ?

And... I can do something like what I want with 'w_debug' with stringstream :

1
2
stringstream os;
os <<data1<<data2;


I want to do something similar

Thanks
Last edited on
Just don't create it as const... how was it declared?
Thanks Bazzy
I have discover what is the reason of the error.
I have w_debug into a functions with 'const' sufix.

Now I have a doubt about the functionality of const functions.
Are related with a faster and powerfull program?
(That is to say, the code compiled are better if you write const variables and functions ?)
Can you give me a last help ?
Thanks
const functions are needed when you have to provide those functions for a const object or reference.
A const object may not be really useful but a const reference is.
When you have a large object, it's good practice to pass it to functions by const reference ( to avoid an expensive copy ), this const reference can only execute const method so you need to properly const-overload your member functions.
eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
struct dummy_vector
{
     int *buffer; // this would be a dynamic array, copying a dummy_vector object means copying the array - expensive

     // ... imagine some expensive copy-constructor implemented here ...
     
     int& operator[] ( int i ) // non-const method, can modify the object but can only be called by non-const objects
     {
          return buffer[i];
     }

     const int& operator[] ( int i ) // const method, cannot modify the object but can be called by const objects
     {
          return buffer[i];
     }
};

void foo ( dummy_vector v ) // dummy_vector passed by value, expensive copy
{
     cout << v[0]; // calls the non-const overload
}

void bar ( const dummy_vector& v ) // by const reference, no copying
{
     cout << v[0]; // calls the const overload. It accesses the object but you are sure that it cannot be modified
}
In the code above, foo is much less efficient than bar because of the expensive copy of dummy_vector.
bar can only work if dummy_vector is const-correct ( which means that methods that don't modify the object are declared as const )
Of course you can't have only const methods in a class, some of them would modify the object
thank you very very much.
Topic archived. No new replies allowed.