const argument modifier

Hi to all,
Hope you are having a nice day/night

I've been struggling with this code for an hour or so, and I'm not sure what I am doing wrong.

Class Constraint
{
private:
PhysicalConnection *_pconn;
VirtualConnection *_vconn;
ConstraintType _type;

public:
(...)
PhysicalConnection & Physical() { return *_pconn; }
VirtualConnection & Virtual() { return *_vconn; }

Constraint& operator= (const Constraint& c) {
this->_pconn->Set( c.Physical() );
this->_vconn->Set( c.Virtual() );
this->_type = c.Type();
return this;
}
}

bool Constraint::operator==( const Constraint& c)
{
if ( c.Physical().GetNode() == this->_pconn->GetNode() &&
c.Physical().GetRack() == this->_pconn->GetRack() &&
c.Physical().GetSubrack() == this->_pconn->GetSubrack() &&
c.Virtual().Fhierarchy() == this->_vconn->Fhierarchy() &&
c.Virtual().Shierarchy() == this->_vconn->Shierarchy() &&
c.Type() == this->_type
)
return true;
return false;
}

As far as I understand the "const" argument modifier means the argument ("c" in this case will not be modified by the function/method. But the compiler, Qt4.6, is not helping me. It says:

error: passing 'const Constraint' as 'this' argument of 'VirtualConnection& Constraint::Virtual()' discards qualifiers
on every line of the _if_ inside _operator==_ overload method.
can someone give me a lead?, or understand the error?

Thank you,
Celythar
Physical() and Virtual() are not const function, and therefore will
cause an error if you call them on a const Constraint object.

As in reality they do not modify the Constraint object, then change their declaration to:
1
2
3
4
public:
(...)
PhysicalConnection & Physical() const { return *_pconn; }
VirtualConnection & Virtual()const  { return *_vconn; }


The other alternative is make the Constraint object non-const - but that will incur a copying penalty;
bool Constraint::operator==( Constraint& c)

This isn't quite right either;
1
2
3
4
5
6
7
Constraint& operator= (const Constraint& c)
 {
this->_pconn->Set( c.Physical() );
this->_vconn->Set( c.Virtual() );
this->_type = c.Type();
return this; //Error this is a pointer not an object - change to -  return *this.
}
Last edited on
Thanks Gulkan,
I really appreciate your help

Topic archived. No new replies allowed.