overloading

overload greater than operator with or without friend

六眼飞鱼酱① 提交于 2019-12-13 06:07:54
问题 Suppose I have the following class: class Point{ private: int x,y; public: int get_x() const {return x;} int get_y() const {return y;} Point() :x(0),y(0){} Point(int x,int y):x(x),y(y){} Point(const Point& P){ x = P.get_x(); y = P.get_y(); } Point& operator= (const Point& P) { x = P.get_x(); y = P.get_y(); return *this; } friend ostream& operator<<(ostream& os,const Point& P) { os<<"["<<P.get_x()<<", "<<P.get_y()<<"]"; return os; } Point operator - (const Point &P){ return Point(x-P.get_x(),y

python operator overload and friends

拜拜、爱过 提交于 2019-12-13 05:24:43
问题 I wanted to know if anyone could give me an example of how to overload an operator within a class, in my case the + operator. Could I define a function (not a class method) that does it? I'm a newbie so I don't really know how to do it. Thanks 回答1: class MyNum(object): def __init__(self, val): super(MyNum,self).__init__() self.val = val def __add__(self, num): return self.__class__.(self.val + num) def __str__(self): return self.__class__.__name__ + '(' + str(self.val) + ')' print(MyNum(3) +

How to implement optional parentheses during function call? (function overloading)

折月煮酒 提交于 2019-12-13 05:06:36
问题 My guess is that this is not possible, but I'd like f and f() to do the same thing. var f = function(str){ console.log(str||'foo'); }(); f; // wanted output: 'foo' f(); // wanted output: 'foo' f('bar'); // wanted output: 'bar' Because f is no longer a function definition, it doesn't seem possible to do f() , but maybe I'm missing something. Any suggestions? 回答1: No that is not possible. The parentheses are required to determine that the function should be called. The value of the expression f

Overloading + to add two pointers

ぃ、小莉子 提交于 2019-12-13 03:57:19
问题 I have a String class and I want to overload + to add two String* pointers. something like this doesn't work: String* operator+(String* s1, String* s2); Is there any way to avoid passing by reference. Consider this example: String* s1 = new String("Hello"); String* s2 = new String("World"); String* s3 = s1 + s2; I need this kind of addition to work. Please suggest. 回答1: You can't. You cannot overload operators for built-in types. In any case, why use pointers at all? Avoid dynamic allocation

JavaScript getElementById function overload

混江龙づ霸主 提交于 2019-12-13 03:44:11
问题 I have a problem with old website. All JavaScript code on it use getElemenById function. But tags of site markup doen't have id property, instead they have only name property. Although it still works for IE, browser returns elements even by name property. For all other browsers it's a mistake in JS. I wonder if there any way to overload this function in other browser to make web site compatible to other browsers? 回答1: There's no way to "overload" a function in JavaScript in the sense that you

Can you declare a variable length Generics type declaration?

梦想的初衷 提交于 2019-12-13 03:26:57
问题 I have an overloaded utility method called CheckDuration with following function signatures. private static Action<int> CheckDuration(Action action) private static Action<int> CheckDuration<T>(Action<T> action, T arg) Basically CheckDuration prints on console how long it took to run a method. Now, I would like to check the duration of a method that takes 2 argument. So I would have to create another overloaded CheckDuration with following method signature. private static Action<int>

How to express mixed ad-hoc and parametric polymorphic in typescript?

我的未来我决定 提交于 2019-12-13 03:20:10
问题 I'm not sure if I'm describing the questing currently in the title. What I'm trying to ask comes from the following requirement. I'm trying to make an abstract for states of finite state machines and comes up with the following definition (in typescript) interface IState { send<T, E>(message: T, callback?:(event: E)=>void): IState; } I'm trying to express that a state of the finite state machine should be able to accept messages and return new state, with an optional callback to handle event

Typescript bindings for classes with methods that shadow a parent's methods with different signatures

与世无争的帅哥 提交于 2019-12-13 02:46:19
问题 I'm working on ts-for-gjs. It generates typescript definitions for gjs, which lets you use gobject-introspection libraries, such as Gtk and GStreamer, in a JS runtime (based on Spidermonkey). Many of the introspected classes have pseudo-constructors as static methods, which leads to some of them having one or more static methods with the same names as methods in their parent classes but with different signatures. Experimenting with ways to solve this problem I found that, if I wanted this API

Confuse about Typescript overload

你离开我真会死。 提交于 2019-12-13 00:53:29
问题 All: I am pretty new to TypeScript, when I read the tutorial about function overloading, it gives an example like: function add(arg1: string, arg2: string): string; function add(arg1: number, arg2: number): number; function add(arg1: boolean, arg2: boolean): boolean; function add(arg1: any, arg2: any): any { return arg1 + arg2; } console.log("add(1,1)=" + add(1, 1)); console.log("add(''1'',''1'')=" + add("1", "1")); console.log("add(true,false)=" + add(true, false)); My confuse is: one

C++ inheritance with overloading not compiling?

心已入冬 提交于 2019-12-12 20:45:19
问题 I am making a Poker game in C++, and I am just trying to get started. I need the ability to compare "Hands", to see which one is greater, equal, or lesser. So, I have a Hand class now, and I made two other sub-classes that are called Straight and ThreeOfKind (I will add the rest later. The Hand class has a method called compareTo(Hand* otherHand) , it then checks the hand ranking to see which one is better. Also, with the Straights and Three of a Kinds, you can compare them together when they