operator-overloading

Why strange behaviour with operator()? [duplicate]

我与影子孤独终老i 提交于 2019-12-14 03:29:36
问题 This question already has answers here : My attempt at value initialization is interpreted as a function declaration, and why doesn't A a(()); solve it? (5 answers) Closed 4 years ago . I have simple class, class Func { public: Func() { cout<<"Constructor"<<endl; } int operator()(int) { cout<<"Operator ()"; return 0; } }; When I create it's object by giving parenthesis, Func f(); , it prints nothing, it should print Constructor . But when I create object without parenthesis it prints

Operator Overloading in Delphi

半腔热情 提交于 2019-12-14 00:49:39
问题 is it possible (in Delphi) to overload operators in classes. I've read some time ago it is possible only for records but I found information that for classes too like in code below: type TMyClass = class class operator Implicit(a: Integer): TMyClass; end; class operator TMyClass.Implicit(a: Integer): TMyClass; begin // ... end; It is (modified) from address: http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/operatoroverloads_xml.html But when I try

overload operator+ / char* ch1 + char* ch2

自作多情 提交于 2019-12-14 00:38:15
问题 I've created simple string class with some extra functions - its mainly for learning purposes. Now i want to overload operator + to allow me add two or more char* to my string. this is what i want to make: tom::string TXT; TXT="abcde" + "1234"; cout << TXT << endl; and output have to be: abcde1234 i want to add more than just const char* later like: .. int NUM=34; TXT="abcd"+NUM+"098"; cout << TXT << endl; and output have to be: abcd34098 ive already done similar thing with operator << TXT <<

When using assignment operator overloading, the new object isn't accessible outside of operator=()

◇◆丶佛笑我妖孽 提交于 2019-12-13 23:09:48
问题 I have an implementation of a Queue which requires writing an assignment operator overload. Queue& Queue::operator= (const Queue& rhs){ if(this->head == rhs.head) return *this; Queue * newlist; if(rhs.head == NULL){ // copying over an empty list will clear it. this->clear(); return * newlist; } newlist = new Queue(rhs); cout << "made new queue" << endl; cout << "new list : " << * newlist << endl; return * newlist; } The problem I'm running into is that when I leave this function, the contents

Equality Operator Overloading in Allegro

旧街凉风 提交于 2019-12-13 18:23:46
问题 One of my friends is trying to overload an equality operator for comparing colours in Allegro, however it does not work,He gets the error "no match for operator==" This is overloaded outside the Color class/struct, the overloaded operator function is shown below: typedef ALLEGRO_COLOR Color; bool operator==(const Color& rhs) const { if(_col.a==rhs.a && _col.b==rhs.b && _col.g==rhs.g && _col.r==rhs.r) return true; else return false; } . . . //Data member Color _col Im thinking this does not

Accessing variables using overloading brackets [] in Ruby

爱⌒轻易说出口 提交于 2019-12-13 18:08:06
问题 Hi i want to do the following. I simply want to overload the [] method in order to access the instance variables... I know, it doesn't make great sense at all, but i want to do this for some strange reason :P It will be something like this... class Wata attr_accessor :nombre, :edad def initialize(n,e) @nombre = n @edad = e end def [](iv) self.iv end end juan = Wata.new('juan',123) puts juan['nombre'] But this throw the following error: overload.rb:11:in `[]': undefined method 'iv' for #

Why can I not use operator overloading for classes?

僤鯓⒐⒋嵵緔 提交于 2019-12-13 15:38:01
问题 I'm having a problem with class operators in TDateTime. type TDateTime = class(TObject) public class operator Add(a: TDateTime; b: TTimeSpan): TDateTime; class operator Subtract(a: TDateTime; b: TTimeSpan): TDateTime; end; implementation class operator TDateTime.Add(a: TDateTime; b: TTimeSpan): TDateTime; begin result := TDateTime.Create(a.Ticks + b.Ticks); end; class operator TDateTime.Subtract(a: TDateTime; b: TTimeSpan): TDateTime; begin result := TDateTime.Create(a.Ticks - b.Ticks); end;

Overloading + operator in R S4 classes and Matrix package

偶尔善良 提交于 2019-12-13 15:32:27
问题 I get a weird effect when trying to overload the + operator and using the Matrix package with sparse matrices. I first define a very simple class that does not use the Matrix package but has a + operator. I then sum two sparse matrices. The first M+M addition delivers the expected result but the second throws an error. Here is a very simple code that generates the error: require(Matrix) setClass("TestM",representation(M='numeric')) setMethod("initialize", "TestM", function(.Object,x) {

C++ Insertion operator overload (<<)

我怕爱的太早我们不能终老 提交于 2019-12-13 15:19:30
问题 I'm working through an assignment in which I must overload the insertion operator to take a Node object. I've created the operator overload function outside the class definition, but inside the node.h file. Everything compiles fine, but the overloaded operator is not called, instead I get simple the address of the object. I'm prohibited from modifying the calling code, so any changes must be to the operator overload. My code as it stands right now: /** OPERATOR << ****************************

C++ operator overloading takes pointer type as parameter?

Deadly 提交于 2019-12-13 14:11:26
问题 I'm new to C++ and trying to figure out the differences between pointer and reference. I've just read this short summary. In the article, the author mentioned that day *operator++ (day *d); won't compile (note: day is an enum type) and argued that the parameter for this overloaded operator function must be type T, T&, or T const&, where T is a class or enum type. I assume that pointer is a built-in type rather than a class or enum so it can't be used to overload operators and that operator