overloading

Function Overloading for the standard library functions in C++

戏子无情 提交于 2019-12-23 20:13:18
问题 I have a free function as part of a class. In the constructor for the class i am doing some malloc operations . So in the destructor i am trying to free that memory. But VS10 compiler complains that the free(pointer); doesn't match the signature of the free function of my class. So question is In a class wherein if we have implemented methods which have same names as that of standard library functions . How to call one over the other. Regards, 回答1: You should qualify your call to the function

How do I use PHP's __get() and __set() for defined fields in my class?

冷暖自知 提交于 2019-12-23 20:10:22
问题 I've had a good read of the PHP specs on overloading, and most of the examples appear to be intended to simply allow defining of custom fields (similar to stdClass). But what about the private fields defined in my class? How should these be retrieved/assigned? I could do a switch on possible values and act on certain values: class A { private $name; private $age; public function __get( $var ) { switch ( $var ) { case 'name': return $this->name; break; case 'age': return $this->age+10; // just

Override toString() Javascript On a Single Array Object

狂风中的少年 提交于 2019-12-23 19:57:47
问题 I have the following: var version = [0,3,0]; // Override the version toString method. version.__proto__.toString = function() { return this.join('.'); }; Which does the following version.toString => '0.3.0' JSlint moans that __proto__ is a reserved name - which is correct. I assume I am overloading incorrectly. I do not want to Array.prototype.toString as that'll override all arrays to replace , with .? 回答1: Just set the method on the array directly: var version = [0,3,0]; // Override the

Overload resolution for char*, char array, and string literals using constexpr, SFINAE and/or type_traits

眉间皱痕 提交于 2019-12-23 19:17:46
问题 I have run into an interesting challenge that I have been trying to solve for hours, but after much research and many failed attempts, I find myself asking this question. I would like to write 3 overloaded functions that each take one of the following types: const char* , const char(&)[N] and string literal (e.g. "BOO") . I understand that a string literal is simply a char array, but please bear with me while I explain my approach. The two functions below are able to differentiate between the

c++ problem with function overloading

柔情痞子 提交于 2019-12-23 19:13:48
问题 i have a problem with function overloading. I will show you with some simple example: class A {}; class B : public A{}; void somefunction(A&, A&); void somefunction(B&, B&); void someotherfunction() { ... A& a1 = ... A& a2 = ... ... } Both a1 and a2 are instances of B but somefunction(a1,a2); calls void somefunction(A&, A&); What did i do wrong? I mean polymorphism and overloading are for stuff like that, arent they? edit: Ok now i know it does not work (thanks for your answers). Any solution

Java Class.cast() and Overload

六眼飞鱼酱① 提交于 2019-12-23 18:28:03
问题 I'm trying to code a packet listener for a little server. I'm very new to Java and this is the first time i mess around with networking. The whole idea it's recive the packet, match the packet id with it's class, pass the input stream to the constructor of the packet so it can be constructed and then give it to the packetHander, wich will have an overladed method for each packet. To achive this im using an array that maps the packets ids to the classes of each one, and using a method called

distinguish between read and write using operator [] overloading in c++

大憨熊 提交于 2019-12-23 16:15:29
问题 I got a Security Class that has an array of Predictions - Prediction is a Class, which holds only a double. I want to allow changing the value of the double, but allow only positive values, and when trying to read the double, if the value is uninitialized (equals -1 in my code) throw exception. I also have double operator in Something like that: class Prediction{ double value; public: ..... Prediction::operator double() const { return this->prediction; } Prediction::operator=(const double

Call of overloaded method from generic method issue

送分小仙女□ 提交于 2019-12-23 15:35:58
问题 I've run into interesting thing (works same in both Java and C#). Java code: public class TestStuff { public static void main(String[] args) { Printer p = new PrinterImpl(); p.genericPrint(new B()); } } class PrinterImpl implements Printer { void print(A a) { System.out.println("a"); } void print(B b) { System.out.println("b"); } @Override public <T extends A> void genericPrint(T b) { print(b); } } interface Printer { public <T extends A> void genericPrint(T a); } class A { } class B extends

Working with __get() by reference

女生的网名这么多〃 提交于 2019-12-23 14:23:04
问题 With an example class such as this: class Test{ public function &__get($name){ print_r($name); } } An instance of Test will kick back output as such: $myTest = new Test; $myTest->foo['bar']['hello'] = 'world'; //outputs only foo Is there a way I can get more information about what dimension of the array is being accessed, showing me (from the previous example) that the bar element of foo , and the hello element of bar are being targeted? 回答1: You can't with the current implementation. In

Functions Overloading in interface and classes - how to?

こ雲淡風輕ζ 提交于 2019-12-23 12:50:00
问题 I have this interface : interface IPoint { getDist(): string; getDist(x: number): any; } and I need a class to implement it but I can't get the right syntax to implement the getDist() method in the class.. class Point implements IPoint { // Constructor constructor (public x: number, public y: number) { } pointMethod() { } getDist() { Math.sqrt(this.x * this.x + this.y * this.y); } // Static member static origin = new Point(0, 0); } it says: Class 'Point' declares interface 'IPoint' but does