operator-overloading

Why the overrided operator new isn't call?

独自空忆成欢 提交于 2019-12-23 20:22:42
问题 I run the following code on VS2005: #include <iostream> #include <string> #include <new> #include <stdlib.h> int flag = 0; void* my_alloc(std::size_t size) { flag = 1; return malloc(size); } void* operator new(std::size_t size) { return my_alloc(size); } void operator delete(void* ptr) { free(ptr); } void* operator new[](std::size_t size) { return my_alloc(size); } void operator delete[](void* ptr) { free(ptr); } int main() { std::string str; std::getline(std::cin, str); std::cout << str;

Underscores, names and literal operators

廉价感情. 提交于 2019-12-23 20:15:37
问题 My question regarding underscores in names is partly answered here, but either the answer is incomplete or I do not fully understand it. Sect. 2.14.8.7 of the C++11 standard declares this literal operator as an example: long double operator "" _w(long double); Besides declaring the operator, the standard and its example do two further things that, if viewed separately, each make sense: it begins the name _w with an underscore; and it puts the operator in the global namespace. My question has

Overloading Insertion Operator in C++

江枫思渺然 提交于 2019-12-23 19:11:22
问题 I have a class in which I'm trying to overload the << operator. For some reason, it is not being overloaded. Here is my .h file: friend std::ostream& operator<<(std::ostream&, const course &); //course is my class object name in my .cpp, I have this as my implementation: std::ostream& operator<<(std::ostream &out, const course & rhs){ out << rhs.info; return out; } This should be correct, but when I try to compile it, it says that cout << tmp; is not defined in ostream. I've included iostream

Why does returning *this result in an infinite loop?

我的未来我决定 提交于 2019-12-23 19:03:30
问题 class binaryOperators { public: int i; binaryOperators (int tempI = 0) { i = tempI; } binaryOperators operator+ (const binaryOperators &right); }; binaryOperators binaryOperators :: operator+ (const binaryOperators &right) { return binaryOperators (*this + right.i); } binaryOperators operator+ (const binaryOperators &left, const binaryOperators &right) { return binaryOperators (left.i + right.i); } int main () { binaryOperators obj (10); obj = 11 + obj; obj = obj + 11; return 0; } So, here

How to overload the operator<< from within a namespace

扶醉桌前 提交于 2019-12-23 16:48:27
问题 This is the smallest contained example I can think of. First the header of the class. This class should simply print the one double it contains whenever the << operator is used. #pragma once #ifndef EURO_H #define EURO_H #include <ostream> namespace EU { class Euro final { public: explicit Euro(double value); virtual ~Euro() = default; double getValue() const; friend std::ostream& operator<<(std::ostream &os, const Euro &euro); private: double m_value; }; } #endif // EURO_H Now the .cpp

is the following new overload leaking memory?

末鹿安然 提交于 2019-12-23 15:17:31
问题 I have encountered the following code: class a { public: void * operator new(size_t l, int nb); double values; }; void *a::operator new (size_t l,int n) { return new char[l+ (n>1 ? n - 1 : 0)*sizeof(double)]; } From what I get it is then used to have an array like structure that start at "values": double* Val = &(p->a->values) + fColumnNumber; My question is : is there a memory leak? I am very new to overloading new operator, but I'm pretty sure that the memory allocated is not deallocated

User-defined overloaded operator * with std::chrono::duration

£可爱£侵袭症+ 提交于 2019-12-23 15:14:21
问题 I've created a Frequency class template intended to work in conjunction with std::chrono::duration. A Frequency object stores a number of cycles per unit duration (both using template parameters for their types). The idea is that multiplying a Frequency by a duration produces an object of type Rep. Here's the class definition. I've omitted all but the relevant members. #include <ratio> #include <chrono> using namespace std::chrono; template <typename Rep, typename Period = std::ratio<1>>

How to take address of an overloaded operator defined as a friend in class body in C++

可紊 提交于 2019-12-23 14:53:51
问题 How can I take an address of an overloaded operator defined as a friend in a class body? I tried the following struct S { friend bool operator==(S, S) { return true; } }; int main() { bool (*f)(S, S) = &operator==; } But gcc gives an error test.cc: In function ‘int main()’: test.cc:6:30: error: ‘operator==’ not defined bool (*f)(S, S) = &operator==; ^ which can be fixed by declaring the operator in the (global) namespace: bool operator==(S, S); Is there a way to take the address without

Can conversion functions be non-member functions

徘徊边缘 提交于 2019-12-23 13:08:55
问题 Is it possible to define the casting operator from one type to another type outside of the class definition as a non-member function? I know it is possible for other operators like operator- but It is not possible with cast operators. For example for two classes A and B, I tried to define the casting operator outside of the A and B scopes as follows: operator A(const B& b) { A a(....); return a; } 回答1: No, conversion functions must be member functions. From C++11, [class.conv.fct]/1: A member

why do subscript operators C++ often comes in pair?

岁酱吖の 提交于 2019-12-23 12:29:15
问题 C++ FAQ is defining a template container Matrix to avoid tricky new delete code. Tutorial says that subscript operators often come in pairs ? Why is it so ? T& operator() (unsigned i, unsigned j); T const& operator() (unsigned i, unsigned j) const; Why is it so ? This is also called : const-overloading. FAQ gives clues. Do you have additional comments ? In particular, should mutate() observe certain rules to be used safely on const objects only ? 回答1: To put it simply, because there are two