operator-overloading

C++ stream as a parameter when overloading operator<<

无人久伴 提交于 2019-12-18 00:33:12
问题 I'm trying to write my own logging class and use it as a stream: logger L; L << "whatever" << std::endl; This is the code I started with: #include <iostream> using namespace std; class logger{ public: template <typename T> friend logger& operator <<(logger& log, const T& value); }; template <typename T> logger& operator <<(logger& log, T const & value) { // Here I'd output the values to a file and stdout, etc. cout << value; return log; } int main(int argc, char *argv[]) { logger L; L <<

Overloading output stream operator for vector<T>

断了今生、忘了曾经 提交于 2019-12-17 23:48:00
问题 What is a recommended way to overload the output stream operator? The following can not be done. It is expected that compilation will fail if the operator << is not defined for a type T. template < class T > inline std::ostream& operator << (std::ostream& os, const std::vector<T>& v) { os << "["; for (std::vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { os << " " << *ii; } os << " ]"; return os; } EDIT: It does compile, the problem was unrelated and was in the namespace.

operator new overloading and alignment

我与影子孤独终老i 提交于 2019-12-17 22:30:01
问题 I'm overloading operator new , but I recently hit a problem with alignment. Basically, I have a class IBase which provides operator new and delete in all required variants. All classes derive from IBase and hence also use the custom allocators. The problem I'm facing now is that I have a child Foo which has to be 16-byte aligned, while all others are fine when aligned to 8-byte. My memory allocator however aligns to 8-byte boundaries only by default, so now the code in IBase::operator new

How arrow-> operator overloading works internally in c++?

拜拜、爱过 提交于 2019-12-17 21:41:10
问题 I understand the normal operator overloading. Compiler can translate them to method call directly. I am not very clear about the -> operator. I was writing my first custom iterator and I felt like the need of -> operator. I took a look at the stl source code and implemented my own like it: MyClass* MyClassIterator::operator->() const { //m_iterator is a map<int, MyClass>::iterator in my code. return &(m_iterator->second); } Then I can use an instance of MyClassIterator like: myClassIterator-

Is multiplication always commutative in inexact floating point arithmetic?

 ̄綄美尐妖づ 提交于 2019-12-17 20:51:15
问题 I'm trying to understand some code in the D language runtime. It seems like there are separate functions for the following two things: array1[] += scalar * array2[]; array1[] += array2[] * scalar; Why can't these be done with one function? I thought multiplication was commutative even in inexact floating-point arithmetic. 回答1: I know nothing about the D language, but I'll happily answer the question in your title: Is multiplication always commutative in inexact floating point arithmetic? Up

overloading [][] operators in c++

a 夏天 提交于 2019-12-17 20:42:13
问题 I'm writing a matrix 3x3 class in c++. glm::mat3 provides access to matrix data through the [][] operator syntax. e.g. myMatrix[0][0] = 1.0f; would set the first row, first column entry to 1.0f . I'd like to provide similar access. How can I overload the [][] operator s? I've tried the following, but I get errors: operator name must be declared as a function const real operator[][](int row, int col) const { // should really throw an exception for out of bounds indices return ((row >= 0 && row

Can I use placement new(this) in operator=?

血红的双手。 提交于 2019-12-17 20:35:40
问题 Background: I have a complicated class with many variables. I have a sound and tested copy constructor: Applepie::Applepie( const Applepie &copy) : m_crust(copy.m_crust), m_filling(copy.m_filling) { } Some of the member variable copy constructors called in the intializer list perform allocation. Question: I need to create operator= . Rather than duplicating the existing constuctor with assignment instead of initialization list, and freeing memory that's being replaced, and etc etc etc, can I

template class, friend operator << overload

微笑、不失礼 提交于 2019-12-17 20:29:30
问题 I'm trying to overload the "<<" operator for a template class. I've the definition of the class in a .h file and its implementation in a .cpp file. /tmp/ccjJIJhO.o: In function `main': main.cpp:(.text+0xad): undefined reference to `std::basic_istream<char, std::char_traits<char> >& operator>><int>(std::basic_istream<char, std::char_traits<char> >&, FeatureVector<int>&)' main.cpp:(.text+0xba): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& operator<< <int>(std:

C++ overloading operator with multiple inheritance from templates

倾然丶 夕夏残阳落幕 提交于 2019-12-17 20:14:36
问题 I have an hierarchy that represents some part of an HTTP client and looks like this: typedef list<pair<string, string> > KeyVal; struct Header { string name; string value; ...}; struct Param { string name; string value; ...}; /* Something that contains headers */ template<typename T> class WithHeaders { KeyVal headers; public: virtual T &operator <<(const Header &h) { headers.push_back(pair<string, string>(h.name, h.value)); return static_cast<T&> (*this); } }; /* Something that contains

No implicit conversion in overloaded operator

泪湿孤枕 提交于 2019-12-17 19:38:49
问题 d1 + 4 works but 4 + d1 doesn't even though 4 can be converted implicitly to a GMan. Why aren't they equivalent? struct GMan { int a, b; GMan() : a(), b() {} GMan(int _a) : a(_a), b() {} GMan(int _a, int _b) : a(_a), b(_b) {} GMan operator +(const GMan& _b) { GMan d; d.a = this->a + _b.a; d.b = this->b + _b.b; return d; } }; int main() { GMan d1(1, 2), d(2); GMan d3; d3 = d1 + 4; d3 = 4 + d1; } 回答1: A call x + y is translated by the C++ compiler into either of the following two calls