Re: A question on the RECT struct

Tech-Archive recommends: Fix windows errors by optimizing your registry



Dom wrote:
I hope I'm not a pest, but I guess the question I have is better put
this way: Since there are already 3 overloads for the Rectangle
constructor, why isn't there just a fourth that takes the LTRB args?
I'm just confused about the two calls, one to a constructor, one to a
method.

Here's the declaration for the constructor that takes the top-left coordinates and the size:

public Rectangle(int x, int y, int width, int height);

Here's the declaration for the constructor that takes the top-left and bottom-right coordinates:

public Rectangle(int xLeft, int yTop, int xRight, int yBottom);

Here's some code that uses these constructors:

// initialize with top-left and size:
Rectangle rectA = new Rectangle(10, 20, 30, 30);

// initialize with top-left and bottom-right:
Rectangle rectB = new Rectangle(10, 20, 40, 50);

Now, imagine you're the compiler. In the above code example, how do you know which constructor goes with which assignment?

That should illustrate the issue, but just in case:

The reason that the constructor exists for one and not the other is that both are method signatures that takes four integers and return a Rectangle. You can't have two constructors with identical signatures, so at least one of the methods cannot be a constructor. It has to be a plain static method that returns a Rectangle.

Now, a valid question might be: why not forego the constructor altogether and make things consistent by making all of the initialization methods be plain static methods.

IMHO, the answer to that is that a constructor really is a preferable way to initialize an object (it's a lot more discoverable, especially via Intellisense, for one), and so it's worth making as many of the initialization methods be constructors.

That's one possible answer. I don't know if that's the actual reason for the current design, but it seems plausible to me.

Pete
.



Relevant Pages

  • Re: Problem with linker
    ... but to have actually written a default constructor. ... such as overloading on int and pointer types. ... conversion of 0 to CString requires a user-defined conversion, ... acceleration operator *(distance d, time_squared t2); ...
    (microsoft.public.vc.mfc)
  • FAQ Suggestions
    ... It can only be applied to reference type variables converting to ... The .NET runtime can't guarantee that parameterless constructors will be ... performing by not having to call constructor code. ... number like casting an int to short) are always explicit. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Languages for embedded
    ... calling constructors of the structure's members. ... If I want to know whether xyz had a constructor, ... On the other hand, if I have a C struct xyz, and the API provides a ... If function ftakes an int by value, it can't modify any int value ...
    (comp.arch.embedded)
  • Re: How to escape the Ocamls superfluous parentheses and type declarations?
    ... > The constructor Mycons expects 2 argument, ... constructor applied to zero or more arguments. ... >> Because you haven't declared the union of char and int. ...
    (comp.lang.ml)
  • Re: Creating an object that is read from an input stream.
    ... > The Box class has a default constructor that leaves it's members undefined. ... the default constructor can put it into a valid state then. ... > int left, right, up, down; ... By 'invalid' do you mean uninitialized, or not initialized with the right values (if the default ...
    (comp.lang.cpp)