Static.Mike
Static.Mike

Reputation: 105

How does that casting work?

I'm doing an exercise for my uni class. I've stumbled upon a following line of code:

std::cout << (const MaszynaStanow&)m << std::endl;

where m is an object of a class.

It doesn't compile. I assume it's some kind of a casting of an object to a constant reference, right?

I've also written an operator function for "<<" overloading so that I could print out the values held by an object like so:

std::cout << m;

I'm getting the following error upon compilation:

.main.cpp:41:13: error: invalid operands to binary expression('ostream'(aka 'basic_ostream<char>') and 'const MaszynaStanow')
std::cout << (const MaszynaStanow&)m << std::endl;
~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~

Which makes me think that my operator overloading function is not suitable in this case(?)

ostream & operator<<(ostream & stream, MaszynaStanow & obj){
    cout<<"MaszynaStanow:"<<endl;
    for (int i = 0; i < obj.size; ++i){
        stream <<i<<" "<<obj.Next[i]->returnName();
        if (i == obj.chosenMode) cout <<" <";
        cout<<endl;
    }
    return stream;
}

I would appreciate any kind of help- even a small hint.

Upvotes: 3

Views: 99

Answers (2)

Baltasarq
Baltasarq

Reputation: 12202

It seems that the programmer is trying to simulate a pass-by-const-reference by doing a previous cast to the object. However, the true place to do any modification is here:

ostream & operator<<(ostream & stream, MaszynaStanow & obj){
    cout<<"MaszynaStanow:"<<endl;
    for (int i = 0; i < obj.size; ++i){
        stream <<i<<" "<<obj.Next[i]->returnName();
        if (i == obj.chosenMode) cout <<" <";
        cout<<endl;
    }
    return stream;
}

While you are going to dump obj to the stream stream, which presumably does not require any modification of obj, you are passing a non-constant reference. Fix the code this way:

ostream & operator<<(ostream & stream, const MaszynaStanow & obj){
    cout<<"MaszynaStanow:"<<endl;
    for (int i = 0; i < obj.size; ++i){
        stream <<i<<" "<<obj.Next[i]->returnName();
        if (i == obj.chosenMode) cout <<" <";
        cout<<endl;
    }
    return stream;
}

Now you are passing obj by reference, but this reference is special: while it s very efficient, it is equivalent (because of the use of const) to a pass-by-value.

Hope this helps.

Upvotes: 1

Mateusz Grzejek
Mateusz Grzejek

Reputation: 12058

You cast explicitly to a const, but your operator is only able to work with non-const instances of MaszynaStanow. Your operator should have the following form:

ostream & operator<<(ostream & stream, const MaszynaStanow & obj)
                                       ^^^^^
                                       you missed this

If you need the object to be modifiable, keep the current version and do not cast the object to a const one. Although I would not expect operator<< to modify its right-handled operand, so the const should be probably there.

Upvotes: 5

Related Questions