operator-keyword

c++ pointers to operators

空扰寡人 提交于 2019-12-03 11:29:57
I want to write a pointer in c++ (or in c++0x), that will points to a operator of a class lets say A or B. Is there any method to do it? Of course there is a syntax like int (A::*_p) (); but it doesn't solve this problem. I want to make general pointer, not specifying the base class for it - only pointer for "operator function" #include <thread> #include <iostream> using namespace std; class A { public: int operator()() { return 10; } }; class B { public: int operator()() { return 11; } }; int main() { A a; int (*_p) (); _p = a.operator(); cout << _p(); B b; _p = b.operator(); cout << _p(); }

How to write a static python getitem method?

落爺英雄遲暮 提交于 2019-12-03 11:05:49
What do I need to change to make this work? class A: @staticmethod def __getitem__(val): return "It works" print A[0] Note that I am calling the __getitem__ method on the type A . When an object is indexed, the special method __getitem__ is looked for first in the object's class. A class itself is an object, and the class of a class is usually type . So to override __getitem__ for a class, you can redefine its metaclass (to make it a subclass of type ): class MetaA(type): def __getitem__(cls,val): return "It works" class A(object): __metaclass__=MetaA pass print(A[0]) # It works In Python3 the

implementing a cast operator in a generic abstract class

亡梦爱人 提交于 2019-12-03 10:48:58
I'm trying to be lazy and implement the cast operators in the abstract base class rather than in each of the derived concrete classes. I've managed to cast one way, but I'm unable to cast the other. I think it might not be possible, but wanted to pick the collective SO mind before giving up: public interface IValueType<T> { T Value{ get; set; } } public abstract class ValueType<T> : IValueType<T> { public abstract T Value { get; set; } public static explicit operator T(ValueType<T> vt) { if(vt == null) return default(T); return vt.Value; } public static implicit operator ValueType<T>(T val) {

Passing operator as a parameter

帅比萌擦擦* 提交于 2019-12-03 10:45:20
问题 I want to have a function that evaluates 2 bool vars (like a truth table) for example: since T | F : T then myfunc('t', 'f', ||); /*defined as: bool myfunc(char lv, char rv, ????)*/ should return true; how can I pass the third parameter? (I know is possible to pass it as a char* but then I will have to have another table to compare operator string and then do the operation which is something I would like to avoid) Is it possible to pass an operator like ^(XOR) or ||(OR) or &&(AND), etc in a

operator std::string() const?

∥☆過路亽.° 提交于 2019-12-03 07:34:27
问题 Can somebody tell me what precisely operator std::string() stands for? 回答1: It is a conversion operator that allows the object to be explicitly or implicitly casted to std::string. When such a cast occurs, the operator is invoked and the result of the cast is the result of the invocation. As an example of an implicit cast, suppose you had a function that accepted type std::string or const std::string& , but not the given object type. Passing your object to that function would result in the

Multiple conditions in ternary operators

限于喜欢 提交于 2019-12-03 07:10:06
问题 First off, the question is "Write a Java program to find the smallest of three numbers using ternary operators." Here's my code: class questionNine { public static void main(String args[]) { int x = 1, y = 2, z = 3; int smallestNum; smallestNum = (x<y && x<z) ? x : (y<x && y<z) ? y : (z<y && z<x) ? z; System.out.println(smallestNum + " is the smallest of the three numbers."); } } I tried to use multiple conditions in the ternary operator but that doesn't work. I was absent a few days so I'm

Why overload true and false instead of defining bool operator?

心不动则不痛 提交于 2019-12-03 05:01:33
问题 I've been reading about overloading true and false in C#, and I think I understand the basic difference between this and defining a bool operator. The example I see around is something like: public static bool operator true(Foo foo) { return (foo.PropA > 0); } public static bool operator false(Foo foo) { return (foo.PropA <= 0); } To me, this is the same as saying: public static implicit operator bool(Foo foo) { return (foo.PropA > 0); } The difference, as far as I can tell, is that by

How to use comparison operators like >, =, < on BigDecimal

半世苍凉 提交于 2019-12-03 04:50:34
问题 I have a domain class with unitPrice set as BigDecimal data type. Now I am trying to create a method to compare price but it seems like I can't have comparison operators in BigDecimal data type. Do I have to change data type or is there other way around? 回答1: Every object of the Class BigDecimal has a method compareTo you can use to compare it to another BigDecimal. The result of compareTo is then compared > 0 , == 0 or < 0 depending on what you need. Read the documentation and you will find

Is it possible to write auto-cast operator outside a struct?

ⅰ亾dé卋堺 提交于 2019-12-03 04:45:17
The exact situation is next: I have defined in system API structs CGPoint and CGSize , and I want to be able to write my_point = my_size . I can't modify CGPoint struct, only can write external operator. I can write binary operators ( + , - , ...) but operator= must by declared inside struct. So is there any other solution? To make the expression a = b; compile you need to either have an operator= in the type of a that takes an element of the type of b , or a type implicitly convertible from b . The first case is ruled out, since operator= must be a member of the class, and since you cannot

Understanding infix method call and cons operator(::) in Scala

◇◆丶佛笑我妖孽 提交于 2019-12-03 03:47:48
问题 I'm quite new to Scala programming language, and was trying something out stucked in my mind while I was following the lecture notes at here. I think I couldn't really understand how cons operator works, here are some things I tried: I've created a pseudo-random number generator, then tried to create a list of one random value: scala> val gen = new java.util.Random gen: java.util.Random = java.util.Random@1b27332 scala> gen nextInt 3 :: Nil <console>:7: error: type mismatch; found : List[Int]