overloading

C++ operator overloading adding 3 vectors together

我只是一个虾纸丫 提交于 2020-05-17 08:09:37
问题 For now this is how I add 3 vectors of type Product together: vector1.insert(std::end(vector1), std::begin(vector2), std::end(vector2)); vector1.insert(std::end(vector1), std::begin(vector3), std::end(vector3)); How do I use operator overloading (I assume overloading the + and = operators) to simplify my code? Product has the following properties: private: std::string url; double cost; std::string name; std::string site; 回答1: Operating overloading is just a normal free function, or member

Overload decorator in typings module doesn't seem to behave as expected

若如初见. 提交于 2020-05-15 11:40:09
问题 >>> from typing import overload >>> @overload ... def hello(s: int): ... return "Got an integer!" >>> def hello(s: str): ... return "Got a string" Why does the calling hello(1) call the function with the string argument? Ideally, the @overload operator should handle it, right? 回答1: Unfortunately, python does not allow function overloading. Each time you think you are overloading function, you are just overwriting previous function declaration. Quote from the docs: The @overload decorator

Why isn't '|' being overloaded?

≯℡__Kan透↙ 提交于 2020-05-14 19:10:25
问题 The following code does not work as expected. What am I missing? use strict; use warnings; use overload '|' => sub { 1 / ( 1 / $_[0] + 1 / $_[1] ) }; print( 5 | 5 ); # Prints '5' instead of '2.5' 回答1: overload works only on blessed references ("objects"). package MyNumber; use strict; use warnings; use overload '|' => sub { 1 / ( 1 / +$_[0] + 1 / +$_[1] ) }, '0+' => sub { $_[0]->{value} }, # Cast to number fallback => 1; # Allow fallback conversions # "Constructor", bless number as MyNumber

How to apply overloaded polymorphed function on elements of base class pointer vector

ぐ巨炮叔叔 提交于 2020-04-16 08:15:47
问题 I have a base class Object : struct Object{ }; and n (in this case 2) classes that inherit from this struct Integer : public Object{ int i_; Integer(int i) : i_{i}{} } struct Float : public Object{ float f_; Float(float f) : f_{f}{} } By (ab-)using polymorphism I can now store those two types in a vector: std::vector<Object*> object_list{new Integer(1), new Float(2.1), new Integer(3), new Float(4.2)}; But now I would like to add all those values together. I can think of... 1) ...defining

How to operate overload “>>”?

匆匆过客 提交于 2020-04-14 12:15:50
问题 Product **products; int numProducts = 0; void setup() { ifstream finput("products.txt"); //get # of products first. finput >> numProducts; products = new Product* [numProducts]; //get product codes, names & prices. for(int i=0; i<numProducts; i++) { products[i] = new Product; finput >> products[i]->getCode() >> products[i]->getName() >> products[i]->getPrice(); } } I am getting an "invalid operands to binary expression" error for this line: finput >> products[i]->getCode() >> products[i]-

How to operate overload “>>”?

扶醉桌前 提交于 2020-04-14 12:14:23
问题 Product **products; int numProducts = 0; void setup() { ifstream finput("products.txt"); //get # of products first. finput >> numProducts; products = new Product* [numProducts]; //get product codes, names & prices. for(int i=0; i<numProducts; i++) { products[i] = new Product; finput >> products[i]->getCode() >> products[i]->getName() >> products[i]->getPrice(); } } I am getting an "invalid operands to binary expression" error for this line: finput >> products[i]->getCode() >> products[i]-

__add__ to support addition of different types?

一个人想着一个人 提交于 2020-04-13 07:15:24
问题 Would be very easy to solve had python been a static programming language that supported overloading. I am making a class called Complex which is a representation of complex numbers (I know python has its own, but i want to make one myself), where a is the real number and b is the imaginary ( Complex(a, b) ). It should support adding Complex instances together ( Complex(2, 4) + Complex(4, 5) = Complex(6, 9) ), as well as adding an integer ( Complex(2, 3) + 4 = Complex(6, 3) ). However, due to

Multiple Default Constructors

╄→гoц情女王★ 提交于 2020-04-11 08:38:11
问题 From this stack overflow question the answer contains this quote: ... definition says that all default constructors (in case there are multiple) ... How can there be multiple default constructors, and why may that be useful or allowed by the standard? 回答1: Default constructors don't have to have no parameters; they just need to be invocable with no arguments. This condition is fulfilled by any constructor whose arguments all have defaults. [class.dtor/1]: A default constructor for a class X

Why does the line marked with //1 print 57 instead of 39?

戏子无情 提交于 2020-03-28 06:55:50
问题 class X { protected int v = 0; public X() { v += 10; } public void proc(X p) { System.out.println(43); } } class Y extends X { public Y() { v += 5; } public void proc(X p) { System.out.println(57); } public int getV() { return v; } } class Z extends Y { public Z() { v += 9; } public void proc(Z p) { System.out.println(39); } } class Main { public static void main(String[] args) { X x = new Z(); Y y = new Z(); Z z = new Z(); x.proc(z);// 1 System.out.println(y.getV()); } } From what I can

Scala: arrays and type erasure

白昼怎懂夜的黑 提交于 2020-02-27 17:30:05
问题 I'd like to write overloaded functions as follows: case class A[T](t: T) def f[T](t: T) = println("normal type") def f[T](a: A[T]) = println("A type") And the result is as I expected: f(5) => normal type f(A(5)) => A type So far so good. But the problem is the same thing doesn't work for Arrays: def f[T](t: T) = println("normal type") def f[T](a: Array[T]) = println("Array type") Now the compiler complains: double definition: method f:[T](t: Array[T])Unit and method f:[T](t: T)Unit at line 14