overload

c++ less operator overload, which way to use?

匿名 (未验证) 提交于 2019-12-03 03:04:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: For example: in a C++ header file, if I defined a struct Record and I would like to use it for possible sorting so that I want to overload the less operator . Here are three ways I noticed in various code. I roughly noticed that: if I'm going to put Record into a std::set , map , priority_queue , … containers, the version 2 works (probably version 3 as well); if I'm going to save Record into a vector<Record> v and then call make_heap(v.begin(), v.end()) etc.. then only version 1 works. struct Record { char c; int num; //version 1 bool

TypeScript Constructor Overload with Empty Constructor

匿名 (未验证) 提交于 2019-12-03 03:04:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Why is it not allowed to have separate constructor definitions in TypeScript ? To have e.g. two constructors , I need to write my code like this. constructor(id: number) constructor(id: number, name?: string, surname?: string, email?: string) { this.id = id; this.name = name; this.surname = surname; this.email = email; } Thereby I need to put ? after the things I want to be not required. Why can't I write it like this? constructor(id: number) { this.id = id; } constructor(id: number, name: string, surname: string, email: string) { this.id =

Method overload resolution in java

匿名 (未验证) 提交于 2019-12-03 03:02:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Here is what I know about overload resolution in java: The process of compiler trying to resolve the method call from given overloaded method definitions is called overload resolution. If the compiler can not find the exact match it looks for the closest match by using upcasts only (downcasts are never done). Here is a class: public class MyTest { public static void main(String[] args) { MyTest test = new MyTest(); Integer i = 9; test.TestOverLoad(i); } void TestOverLoad(int a){ System.out.println(8); } void TestOverLoad(Object a){ System

C++ overload operator twice, one return non-const reference and the other const reference, what is the preference?

匿名 (未验证) 提交于 2019-12-03 02:49:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I overload an operator twice with the same parameter list. but with different return type: T& operator()(par_list){blablabla} const T& operator()(par_list){blablabla} So when I'm calling the () operator, which function would be called based on what preference or situation? I know that if I call () under const function it has to be the const T& one. I'm just curious how C++ deal with such situation and how the default preference works. Thanks 回答1: These functions don't overload each other; they have the same signatures, and so the attempt to

How to overload constructor of an Object in JS (Javascript)?

匿名 (未验证) 提交于 2019-12-03 02:45:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Can I do something like?: function User(form) { this._username = form.username.value; this._password = form.password.value; this._surname = form.surname.value; this._lastname = form.lastname.value; this._birthdate = form.b_day.value+"-"+form.b_month.value+"-"+form.b_year.value; this._avatar = form.avatar; this._messages = new Array(); this._messagesCount=0; } function User(userName,password,surname,lastName,birthdate) { this._username = userName; this._password = password; this._surname = surname; this._lastname = lastName; this._birthdate =

Unable to overload function in viewDidLoad() in swift

匿名 (未验证) 提交于 2019-12-03 02:33:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Unable to overload function in viewDidLoad() in swift. It gives error definition conflict with previous value" at "func joinString(#strings: String...) -> String { override func viewDidLoad() { super.viewDidLoad() func joinString(#strings: String[]) -> String { var str = "" for s in strings { str += s } return str } func joinString(#strings: String...) -> String { return joinString(strings: strings) } println(joinString(strings : ["I", "am", "an", "array"])) println(joinString(strings : "I", "am", "an", "array")) } 回答1: This looks like a

PHP - Indirect modification of overloaded property

匿名 (未验证) 提交于 2019-12-03 02:14:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I know this question has been asked several times, but none of them have a real answer for a workaround. Maybe there's one for my specific case. I'm building a mapper class which uses the magic method __get() to lazy load other objects. It looks something like this: public function __get ( $index ) { if ( isset ($this->vars[$index]) ) { return $this->vars[$index]; } // $index = 'role'; $obj = $this->createNewObject ( $index ); return $obj; } In my code I do: $user = createObject('user'); $user->role->rolename; This works so far. The User

no overload for matches delegate &#039;system.eventhandler&#039;

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: As I'm pretty new to C#, I struggle with the following piece of code. When I click to button 'knop', the method 'klik' has to be executed. The method has to draw the Bitmap 'b', generated by 'DrawMandel' on the form. But I constantly get the error 'no overload for matches delegate 'system.eventhandler'. using System; using System.Windows.Forms; using System.Drawing; class Mandelbrot : Form { public Bitmap b; public Mandelbrot() { Button knop; knop = new Button(); knop.Location = new Point(370, 15); knop.Size = new Size(50, 30); knop.Text =

C++ template functions overload resolution

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have the following code: #include <iostream> template < typename T > void f ( T ) { std :: cout << "f(T)" << std :: endl ; } template < typename T > void f ( bool ) { std :: cout << "f(bool)" << std :: endl ; } int main ( ) { f ( true ); // #1 prints f(T) f <bool> ( true ); // #2 prints f(bool) } The #1 line calls f(T) , while #2 line calls f(bool) . Why does this happen? And what are the rules for selecting an overloaded template function? UPDATE I understood that in the first call compiler is just unable to deduce T while

What&#039;s the right way to overload operator== for a class hierarchy?

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Suppose I have the following class hierarchy: class A { int foo; virtual ~A() = 0; }; A::~A() {} class B : public A { int bar; }; class C : public A { int baz; }; What's the right way to overload operator== for these classes? If I make them all free functions, then B and C can't leverage A's version without casting. It would also prevent someone from doing a deep comparison having only references to A. If I make them virtual member functions, then a derived version might look like this: bool B::operator==(const A& rhs) const { const B* ptr =