operator-overloading

Overriding instance variable array's operators in Ruby

北城以北 提交于 2019-12-21 05:47:05
问题 Sorry for the poor title, I don't really know what to call this. I have something like this in Ruby: class Test def initialize @my_array = [] end attr_accessor :my_array end test = Test.new test.my_array << "Hello, World!" For the @my_array instance variable, I want to override the << operator so that I can first process whatever is being inserted to it. I've tried @my_array.<<(value) as a method in the class, but it didn't work. 回答1: I think you're looking for this: class Test def initialize

How do I declare an overloaded operator in an abstract class and override it in a derived non-abstract class?

扶醉桌前 提交于 2019-12-21 05:19:07
问题 I'm trying to write an abstract class with some pure virtual binary operators, which should be implemented by the derived class in order to accomplish operator polymorphism. Here's a simplified example: class Base { public: virtual const Base& operator+ (const Base&) const = 0; }; class Derived : public Base { public: const Derived& operator+ (const Derived&) const; }; const Derived& Derived::operator+ (const Derived& rvalue) const { return Derived(); } It doesn't matter right now what the

Few doubts about casting operators in C++

筅森魡賤 提交于 2019-12-21 04:51:33
问题 The reinterpret_cast as we know can cast any pointer type to any another pointer type. The question I want to ask regarding this cast operator are: How does reinterpret_cast work, What is the magic(the internal implementation) that allows reinterpret_cast to work? How to ensure safety when using reinterpret_cast ? As far as i know, it doesn't guarantee of safe casting, So what precaution to take while using reinterpret_cast? What is the practical usage of this operator. I have not really

Few doubts about casting operators in C++

 ̄綄美尐妖づ 提交于 2019-12-21 04:51:10
问题 The reinterpret_cast as we know can cast any pointer type to any another pointer type. The question I want to ask regarding this cast operator are: How does reinterpret_cast work, What is the magic(the internal implementation) that allows reinterpret_cast to work? How to ensure safety when using reinterpret_cast ? As far as i know, it doesn't guarantee of safe casting, So what precaution to take while using reinterpret_cast? What is the practical usage of this operator. I have not really

How to overload |= operator on scoped enum?

笑着哭i 提交于 2019-12-21 03:12:05
问题 How can I overload the |= operator on a strongly typed (scoped) enum (in C++11, GCC)? I want to test, set and clear bits on strongly typed enums. Why strongly typed? Because my books say it is good practice. But this means I have to static_cast<int> everywhere. To prevent this, I overload the | and & operators, but I can't figure out how to overload the |= operator on an enum . For a class you'd simply put the operator definition in the class, but for enums that doesn't seem to work

overloading + and += operators for “Number Classes”

喜夏-厌秋 提交于 2019-12-20 20:16:12
问题 I want to create extension functions for classes that encapsulate simple Number s. For example DoubleProperty . I encountered the problem, that I can't overload the + and the += operator at the same time. I wan't to create a behaviour, that passes following tests: class DoublePropertyTest { lateinit var doubleProperty: DoubleProperty @Before fun initialize() { doubleProperty = SimpleDoubleProperty(0.1) } @Test fun plus() { val someProperty = doubleProperty + 1.5 assertEquals(someProperty

Unclear use of operator double()

你。 提交于 2019-12-20 17:59:52
问题 I have a Rectangle class with conversion operators to both double and std::string : class Rectangle { public: Rectangle(double x, double y) : _x(x), _y(y) {} operator std::string (); operator double (); private: double _x, _y; double getArea() {return _x * _y;} }; int main() { Rectangle r(3, 2.5); cout << r << endl; return 0; } I don’t understand why operator double() is invoked, rather than operator std::string() . As far as I know, according to C++ wikibook, operator double is used to

Why friend function is preferred to member function for operator<<

白昼怎懂夜的黑 提交于 2019-12-20 16:20:55
问题 When you are going to print an object, a friend operator<< is used. Can we use member function for operator<< ? class A { public: void operator<<(ostream& i) { i<<"Member function";} friend ostream& operator<<(ostream& i, A& a) { i<<"operator<<"; return i;} }; int main () { A a; A b; A c; cout<<a<<b<<c<<endl; a<<cout; return 0; } One point is that friend function enable us to use it like this cout<<a<<b<<c What other reasons? 回答1: You have to use a free function and not a member function as

Python overloading multiple getitems / index requests

拥有回忆 提交于 2019-12-20 12:45:14
问题 I have a Grid class which I want to access using myGrid[1][2] . I know I can overload the first set of square brackets with the __getitem__() method, but what about the second. I thought I could achieve this by having a helper class which also implements __getitem__ and then: class Grid: def __init__(self) self.list = A TWO DIMENSIONAL LIST ... def __getitem__(self, index): return GridIndexHelper(self, index) class GridIndexHelper: def __init__(self, grid, index1): self.grid = grid self

ruby operator overloading question

ぐ巨炮叔叔 提交于 2019-12-20 12:30:28
问题 i've been messing around with ruby and opengl for entertainment purposes, and i decided to write some 3d vector/plane/etc classes to pretty up some of the math. simplified example: class Vec3 attr_accessor :x,:y,:z def *(a) if a.is_a?(Numeric) #multiply by scalar return Vec3.new(@x*a, @y*a, @z*a) elsif a.is_a?(Vec3) #dot product return @x*a.x + @y*a.y + @z*a.z end end end v1 = Vec3.new(1,1,1) v2 = v1*5 #produces [5,5,5] which all fine and dandy, but i also want to be able to write v2 = 5*v1