digitalmars.D - <Novice> Operator overloading
- "aelmetwaly" <aelmetwaly gawab.com> Nov 04 2004
- Ant <Ant_member pathlink.com> Nov 05 2004
- "Thomas Kuehne" <thomas-dloop kuehne.cn> Nov 05 2004
- Ant <duitoolkit yahoo.ca> Nov 05 2004
- Sjoerd van Leent <svanleent wanadoo.nl> Nov 06 2004
- "Thomas Kuehne" <thomas-dloop kuehne.cn> Nov 06 2004
- Ben Hinkle <bhinkle4 juno.com> Nov 05 2004
- Ant <duitoolkit yahoo.ca> Nov 05 2004
- "aelmetwaly" <aelmetwaly gawab.com> Nov 06 2004
- Stewart Gordon <smjg_1998 yahoo.com> 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...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)?
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. Thomas
Nov 05 2004
On Fri, 05 Nov 2004 23:36:44 +0100, Thomas Kuehne wrote:Ant schrieb: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)?
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. Thomas
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. Ant
Nov 05 2004
Ant wrote:On Fri, 05 Nov 2004 23:36:44 +0100, Thomas Kuehne wrote:Ant schrieb: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)?
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. Thomas
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. Ant
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, Sjoerd
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) # class X { # private int i; # # static void usesX() { # X x = new X(); # int j = x.i; # } # # public static void main(String args[]){ # usesX(); # } #} 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: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
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 06 2004
aelmetwaly wrote:Yes ther compiler crashes after printing the error. and the constructor declared with inout parameters
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









"Thomas Kuehne" <thomas-dloop kuehne.cn> 