I have a class declared in the following way:
#ifndef MYCLASS_H
#define MYCLASS_H
class MyClass : public MyOtherClass
{
public:
MyClass();
int a() const{ return _a; };
int b() const{ return _b; };
private:
int _a;
int _b;
};
inline bool operator==( const MyClass& lhs, const MyClass& rhs )
{
return (lhs.a() == rhs.a()) && (lhs.b() == rhs.b());
}
#endif
My problem is that any breakpoints is set in the overloaded operator== never get hit, with Visual Studio even telling me that execution will never reach this function. I’ve followed this rule of thumb for overloading comparison operators, but it doesn’t mention anything other than to make them non-members, so I’m not sure if I’ve missed something with operator overloading or inline functions.
Can anyone tell me why my breakpoints are never being hit?
Your breakpoints are never hit because the compiler is inlining the code. To set a breakpoint inside that inline function could mean setting hundreds or thousands of “virtual” breakpoints. Keeping track of what code is inlined where is just too much work for the IDE to be doing so, as a result, it doesn’t.
To get round the issue either run in debug (where inlining won’t, I think, occur) or don’t inline the function.
You can also compile with the /Od flag to disable inlining as well as all other optimisations (which is what it does in debug).
Setting the /Ob0 flag should disable inlining from occurring. For performance reasons, however, its advisable to not do this often so you really are best off running in debug.