digitalmars.D - <Novice> Operator overloading
- aelmetwaly (43/43) Nov 04 2004 Hi there,
- Ant (6/18) Nov 05 2004 this is not an answer but another question:
- Thomas Kuehne (6/25) Nov 05 2004 opAdd is a member function of CMyClass. Thus it has access to all privat...
- Ant (8/38) Nov 05 2004 The problem I have is with c.Width: in java it's not visible.
- Sjoerd van Leent (22/69) Nov 06 2004 Maybe I can give some enlightenment here. In Java, each protection
- Thomas Kuehne (19/36) Nov 06 2004 code: (Java)
- Ben Hinkle (7/58) Nov 05 2004 did the compiler really "crash"? or did it just stop compiling because o...
- Ant (13/66) Nov 05 2004 ok, now I have D here.
- aelmetwaly (5/71) Nov 06 2004 Yes ther compiler crashes after printing the error.
-
Stewart Gordon
(5/7)
Nov 08 2004
Hi there, Consider this code in C++ : class CMyClass { private : int Width; int Height; public : CMyClass( int W, int H ) : Width(W), Height(H) {} public : CMyClass operator+ (CMyClass c) { return CMyClass( Width + c.Width, Height + c.Height ); } } I translate this code in D like this : class CMyClass { private : int Width; int Height; public : this(int W, int H) { Width = W; Height = H; } public : CMyClass opAdd(CMyClass c) { // if I write it like this, the compiler cashes and give the following error // error : class constructor call must be in a constructor. // code : return this ( Width + c.Width, Height + c.Height); // and if I write it like this , it gives the following error // error : this.Width + CMyClass.Width is not an lvalue // code :return new CMyClass ( Width + c.Width, Height + c.Height); // I have to write it like this to compile int w = Width + CMyClass.Width; int h = Height + CMyClass.Height; return new CMyClass(w,h); } Is this wright or what?! and Why it gives these errors?
Nov 04 2004
In article <cmf72p$12a0$1 digitaldaemon.com>, aelmetwaly says...Hi there, Consider this code in C++ : class CMyClass { private : int Width; int Height;// I have to write it like this to compile int w = Width + CMyClass.Width; int h = Height + CMyClass.Height; return new CMyClass(w,h); }this is not an answer but another question: aren't Width and Height private? you should not be able to access them at all. what happens if they are not private (or protected)? Ant
Nov 05 2004
Ant schrieb:In article <cmf72p$12a0$1 digitaldaemon.com>, aelmetwaly says...opAdd is a member function of CMyClass. Thus it has access to all private members of CMyClass. Maybe you are confusing Class and Object. "private" blocks on a per class/type level and not on the object level. ThomasHi there, Consider this code in C++ : class CMyClass { private : int Width; int Height;// I have to write it like this to compile int w = Width + CMyClass.Width; int h = Height + CMyClass.Height; return new CMyClass(w,h); }this is not an answer but another question: aren't Width and Height private? you should not be able to access them at all. what happens if they are not private (or protected)?
Nov 05 2004
On Fri, 05 Nov 2004 23:36:44 +0100, Thomas Kuehne wrote:Ant schrieb:The problem I have is with c.Width: in java it's not visible. As I said before, I don't care how it works on C++. (I did confuse the code I cut of with the one I left in...) I don't confuse Class and Object. ;) D confuses class and object! :( I use to like C++ but that was over 10 years (10? anyway, a long time) ago. AntIn article <cmf72p$12a0$1 digitaldaemon.com>, aelmetwaly says...opAdd is a member function of CMyClass. Thus it has access to all private members of CMyClass. Maybe you are confusing Class and Object. "private" blocks on a per class/type level and not on the object level. ThomasHi there, Consider this code in C++ : class CMyClass { private : int Width; int Height;// I have to write it like this to compile int w = Width + CMyClass.Width; int h = Height + CMyClass.Height; return new CMyClass(w,h); }this is not an answer but another question: aren't Width and Height private? you should not be able to access them at all. what happens if they are not private (or protected)?
Nov 05 2004
Ant wrote:On Fri, 05 Nov 2004 23:36:44 +0100, Thomas Kuehne wrote:Maybe I can give some enlightenment here. In Java, each protection attribute is on object level. That is, an instantiated object of type X may not access a protected member of another object of the same type (X). In D, protection attributes are per module basis. This means that any object, method and template can access any protected attribute at any level within the same module. For example, in D the following would work: module x; class X { private int i; static void usesX() { X x; int j = x.i; } } in Java, the same code would (correctly) fail, since usesX is static and may not access the private attribute i. N.B. Protection levels are needed to keep other modules out. This also means that classes are automatically friends of themselves. Regards, SjoerdAnt schrieb:The problem I have is with c.Width: in java it's not visible. As I said before, I don't care how it works on C++. (I did confuse the code I cut of with the one I left in...) I don't confuse Class and Object. ;) D confuses class and object! :( I use to like C++ but that was over 10 years (10? anyway, a long time) ago. AntIn article <cmf72p$12a0$1 digitaldaemon.com>, aelmetwaly says...opAdd is a member function of CMyClass. Thus it has access to all private members of CMyClass. Maybe you are confusing Class and Object. "private" blocks on a per class/type level and not on the object level. ThomasHi there, Consider this code in C++ : class CMyClass { private : int Width; int Height;// I have to write it like this to compile int w = Width + CMyClass.Width; int h = Height + CMyClass.Height; return new CMyClass(w,h); }this is not an answer but another question: aren't Width and Height private? you should not be able to access them at all. what happens if they are not private (or protected)?
Nov 06 2004
Sjoerd van Leent schrieb:Maybe I can give some enlightenment here. In Java, each protection attribute is on object level. That is, an instantiated object of type X may not access a protected member of another object of the same type (X). In D, protection attributes are per module basis. This means that any object, method and template can access any protected attribute at any level within the same module. For example, in D the following would work: module x; class X { private int i; static void usesX() { X x; int j = x.i; } } in Java, the same code would (correctly) fail, since usesX is static and may not access the private attribute i.code: (Java) does compile and behaves like a D class. The access rights are the same for D and Java. The only difference is, that all classes in a D module(one source file) have the same access rights as if they where inner classes in an Java class named like the D module. Thomas
Nov 06 2004
aelmetwaly wrote:Hi there, Consider this code in C++ : class CMyClass { private : int Width; int Height; public : CMyClass( int W, int H ) : Width(W), Height(H) {} public : CMyClass operator+ (CMyClass c) { return CMyClass( Width + c.Width, Height + c.Height ); } } I translate this code in D like this : class CMyClass { private : int Width; int Height; public : this(int W, int H) { Width = W; Height = H; } public : CMyClass opAdd(CMyClass c) {// if I write it like this, the compiler cashes and give the following error // error : class constructor call must be in a constructor. // code : return this ( Width + c.Width, Height + c.Height);did the compiler really "crash"? or did it just stop compiling because of the error? When I try it I get the error and the compiler stops without crashing. This is what I would expect. The error message explains what is wrong with the code.// and if I write it like this , it gives the following error // error : this.Width + CMyClass.Width is not an lvalue // code :return new CMyClass ( Width + c.Width, Height + c.Height);This code works for me. Are you sure you didn't declare the constructor arguments as inout or pointers or something?// I have to write it like this to compile int w = Width + CMyClass.Width; int h = Height + CMyClass.Height; return new CMyClass(w,h); } Is this wright or what?! and Why it gives these errors?
Nov 05 2004
On Fri, 05 Nov 2004 08:37:07 +0200, aelmetwaly wrote:Hi there, Consider this code in C++ : class CMyClass { private : int Width; int Height; public : CMyClass( int W, int H ) : Width(W), Height(H) {} public : CMyClass operator+ (CMyClass c) { return CMyClass( Width + c.Width, Height + c.Height ); } } I translate this code in D like this : class CMyClass { private : int Width; int Height; public : this(int W, int H) { Width = W; Height = H; } public : CMyClass opAdd(CMyClass c) { // if I write it like this, the compiler cashes and give the following error // error : class constructor call must be in a constructor. // code : return this ( Width + c.Width, Height + c.Height); // and if I write it like this , it gives the following error // error : this.Width + CMyClass.Width is not an lvalue // code :return new CMyClass ( Width + c.Width, Height + c.Height); // I have to write it like this to compile int w = Width + CMyClass.Width; int h = Height + CMyClass.Height; return new CMyClass(w,h); } Is this wright or what?! and Why it gives these errors?ok, now I have D here. 1 - return this() is not valid you use "new" to create a new instance 2 - the second version compiles on linux dmd 0.105 and gives the expected you expected. I expected that c.Width and c.Height would not be visible. If I remember correctly every anything on the same scope is accessible indepentently of the access modifier (I don't like that) 3 - the final version adds to itself, ignoring the object c. Prefixing with the class name gives the current object (I think) (I don't like that) 4 - variables should start with a lower case char. Ant
Nov 05 2004
Yes ther compiler crashes after printing the error. and the constructor declared with inout parameters "Ant" <duitoolkit yahoo.ca> wrote in message news:pan.2004.11.06.01.22.10.734425 yahoo.ca...On Fri, 05 Nov 2004 08:37:07 +0200, aelmetwaly wrote:followingHi there, Consider this code in C++ : class CMyClass { private : int Width; int Height; public : CMyClass( int W, int H ) : Width(W), Height(H) {} public : CMyClass operator+ (CMyClass c) { return CMyClass( Width + c.Width, Height + c.Height ); } } I translate this code in D like this : class CMyClass { private : int Width; int Height; public : this(int W, int H) { Width = W; Height = H; } public : CMyClass opAdd(CMyClass c) { // if I write it like this, the compiler cashes and give theerror // error : class constructor call must be in a constructor. // code : return this ( Width + c.Width, Height + c.Height); // and if I write it like this , it gives the following error // error : this.Width + CMyClass.Width is not an lvalue // code :return new CMyClass ( Width + c.Width, Height + c.Height); // I have to write it like this to compile int w = Width + CMyClass.Width; int h = Height + CMyClass.Height; return new CMyClass(w,h); } Is this wright or what?! and Why it gives these errors?ok, now I have D here. 1 - return this() is not valid you use "new" to create a new instance 2 - the second version compiles on linux dmd 0.105 and gives the expected you expected. I expected that c.Width and c.Height would not be visible. If I remember correctly every anything on the same scope is accessible indepentently of the access modifier (I don't like that) 3 - the final version adds to itself, ignoring the object c. Prefixing with the class name gives the current object (I think) (I don't like that) 4 - variables should start with a lower case char. Ant
Nov 06 2004
aelmetwaly wrote:Yes ther compiler crashes after printing the error. and the constructor declared with inout parameters<snip top of upside-down reply> Then why did you tell us in your original post that it isn't? Copy and paste exists for a reason. Stewart.
Nov 08 2004