
Overloading operators in namespaces
It's very common to overload operators for custom C++ classes but sometimes the overloaded operator is not actually used, if the class is defined in different namespace as the overloaded operator.
To fix this, the overloaded operator must be moved to same namespace as the class is defined and where the operator is used, this allows using just the class name and not fully qualified name that includes all the namespaces the class is defined in.
Incorrect:
namespace {
std::ostream& operator<<(std::ostream& os, const CryptoNote::IBlockchainConsumer* consumer) {
return os << "0x" << std::setw(8) << std::setfill('0') << std::hex << reinterpret_cast<uintptr_t>(consumer) << std::dec << std::setfill(' ');
}
}
Correct:
namespace CryptoNote {
std::ostream& operator<<(std::ostream& os, const IBlockchainConsumer* consumer) {
return os << "0x" << std::setw(8) << std::setfill('0') << std::hex << reinterpret_cast<uintptr_t>(consumer) << std::dec << std::setfill(' ');
}
}
Leave Overloading operators in namespaces to:
Read more #development posts
Best Posts From Mika
We have not curated any of mtl1979's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Mika
- How to use MyHeritage Family Tree Builder without subscribing...
- PayPal launches its own stablecoin PYUSD... Is it safe or not?
- Design limitations of responsive websites
- April 1st, 2023 marks end of mainstream support of Ubuntu 18.04
- Understanding the Ethereum London Revised scam...
- How to use PancakeSwap for creating trading pair, swapping, buying and selling tokens - Part 2
- How to use PancakeSwap for creating trading pair, swapping, buying and selling tokens - Part 1
- How to combat abuse of token bridges...
- Finnish food and drink company recalls potato chips due to potential overdose...
- Why it is bad idea to let Ubuntu upgrade Python to 3.10...