Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A QuizGeneral Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Not totally sure about this, but I think this is a scenario Stephen Dewhurst calls the "Operator Function Lookup Anomaly" in C++ Gotchas #23. He calls the explicit form to call an operator overload the "function call syntax" and shows an example in which infix syntax and function call syntax differ with respect to name lookup.
class X { public: X &operator %( const X & ) const; // ... }; X &operator %( const X &, int ); void X::f() { X &anX = *this; anX % 12; // OK, non-member operator %( anX, 12 ); // error! }
The use of the function call syntax follows the standard lookup sequence in searching for the function name. In the case of the member function
X::f
, the compiler will first look in the classX
for a function namedoperator %
. Once it finds the name, it won’t continue looking in outer scopes for additional functions namedoperator %
.
Let's apply this to the operator delete
scenario.
template<typename T>
class E {
public:
E(T* inst) {
// delete inst; // Ok, infix call syntax
// T::operator delete(inst); // Ok, qualified lookup enforced
operator delete(inst); // The "anomaly"! This finds the operator delete for E
}
};
Here, the name lookup stops at the operator delete
that E
provides, which is the default, global implementation. You can verify this by adding an operator delete
member function to E
.
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 10 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 9 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.
manpreet
Best Answer
2 years ago
So i'm trying to create a base class that has a default deallocation function for that type of base classes. Depending on how I delete a derived object I'm seeing different behaviour, maybe anyone can shed some light on why I'm not seeing my override working in the below commented cases where the custom deallocation is not invoked:
I'm guessing that the last attempt (invoking operator delete) without T:: uses the global deallocation function instead of my override, but why is that when
delete inst
is working just fine without having to specify T::?I was expecting all three statements to actually invoke the delete operator for the object if it has been overriden. Can this be controlled through anything or is this correctly following the C++ ABI?