operator-overloading

error: pointer being freed was not allocated

99封情书 提交于 2019-12-19 10:19:49
问题 I am trying to overload the assignment operator to do a deep copy of a polygon object, the program compiles but I am getting an error toward the end that I want to clear up. Below is the relevant code, if you think I need to add more please just post a comment. Assume the proper #include 's and that the << operator is overloaded for proper output etc... The error is: malloc: * error for object 0x1001c0: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug. /

>> and << operator overloading

允我心安 提交于 2019-12-19 09:49:13
问题 I just did a quiz for my programming class and got this question wrong: The return type of the function to overload the operator << must be a reference to an ostream object. This does not seem right at all to me. Surely C++ is a bit more open ended than this. But I thought I'd ask here anyway. How is this right (or wrong)? My C++ knowledge begins to really fade when it comes to operator overloading.. 回答1: It is not required by C++ that the return type be a reference to an ostream object.

ctypes reimplementation of rshift for c_ulong

余生颓废 提交于 2019-12-19 09:17:59
问题 i am accessing a C library via ctypes and i am stuck with the following problem: I am generating a "wrapper" (ctypes commands to access the library with ctypes) using ctypeslib. The C library contains macros which are converted to python functions in this step. (To be independent of the libraries internals as much as possible i want to use some of these macros in python.) One of these macros looks like this: # using the ctypes types myuint16_t = c_ushort myuint32_t = c_ulong def mymacro(x):

Overload -> operator to forward member-access through Proxy

半腔热情 提交于 2019-12-19 09:17:29
问题 I'm trying to wrap a Python PyObject* in an Object class. In Python, everything is a PyObject* . A list is a PyObject* , and each item in the list is itself a PyObject* . Which could even be another list. etc. I'm trying to allow fooList[42] = barObj style syntax by means of a Proxy pattern (here). Now that I have that working, I want to extend it so that fooList[42] can be used as an Object . Specifically I want to be able to handle... fooList[42].myObjMethod() fooList[42].myObjMember = ...

Overload -> operator to forward member-access through Proxy

一世执手 提交于 2019-12-19 09:17:04
问题 I'm trying to wrap a Python PyObject* in an Object class. In Python, everything is a PyObject* . A list is a PyObject* , and each item in the list is itself a PyObject* . Which could even be another list. etc. I'm trying to allow fooList[42] = barObj style syntax by means of a Proxy pattern (here). Now that I have that working, I want to extend it so that fooList[42] can be used as an Object . Specifically I want to be able to handle... fooList[42].myObjMethod() fooList[42].myObjMember = ...

overloading “<<” with a struct (no class) cout style

倖福魔咒の 提交于 2019-12-19 07:52:54
问题 I have a struct that I'd like to output using either 'std::cout' or some other output stream. Is this possible without using classes? Thanks #include <iostream> #include <fstream> template <typename T> struct point{ T x; T y; }; template <typename T> std::ostream& dump(std::ostream &o,point<T> p) const{ o<<"x: " << p.x <<"\ty: " << p.y <<std::endl; } template<typename T> std::ostream& operator << (std::ostream &o,const point<T> &a){ return dump(o,a); } int main(){ point<double> p; p.x=0.1; p

Function call operator [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-19 07:39:33
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: C++ Functors - and their uses. Why override operator() ? I've seen the use of operator() on STL containers but what is it and when do you use it? 回答1: That operator turns your object into functor. Here is nice example of how it is done. Next example demonstrates how to implement a class to use it as a functor : #include <iostream> struct Multiply { double operator()( const double v1, const double v2 ) const {

Operator new[] does not receive extra bytes

限于喜欢 提交于 2019-12-19 06:39:32
问题 I have such code #include <cstdlib> class Foo { int m_data; public : Foo() : m_data(0) { } /*~Foo() { }*/ static void* operator new[](const size_t size) { return malloc(size); } static void operator delete[](void* data) { free(data); } }; int main() { Foo* objects = new Foo[5]; delete [] objects; } In this case I receive value of size in operator new overloading as 20 bytes as I wanted ( sizeof(int) * 5 ). But if I uncomment the destructor I get size as 24 bytes. Yeah, I now that these extra

Implicit conversion with operator

冷暖自知 提交于 2019-12-19 05:33:15
问题 This is in part inspired by this question. When I write the code: void test(std::string inp) { std::cout << inp << std::endl; } int main(void) { test("test"); return 0; } "test" is implicitly converted from const char* to std::string , and I get the expected output. However, when I try this: std::string operator*(int lhs, std::string rhs) { std::string result = ""; for(int i = 0; i < lhs; i++) { result += rhs; } return result; } int main(void) { std::string test = 5 * "a"; return 0; } I get

Overloading logical operators considered bad practice?

最后都变了- 提交于 2019-12-19 05:19:43
问题 Is it a bad idea to overload &&, || or comma operator and Why? 回答1: I wouldn't overload operator&& or operator|| . Even if you define a class that gives rise to a Boolean algebra (finite sets, for example), it would probably be a better choice to overload operator& and operator| . The reason is that C++ programmers expect special semantics for operator&& and operator|| : they are short-circuited , i.e. don't evaluate their right-hand argument if not necessary. You can't get this behavior by