Re: Best Practices: always use new() with objects?

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



On Aug 12, 6:44 am, GlennDoten <gdo...@xxxxxxxxx> wrote:


I kinda have a feeling you are trying to ask a different question here,
or I have misinterpreted what you are asking.

--
-glenn-

No, you answered my question glenn.

But I posted this before I realized that in C# there is never any
implicit shallow copying (unlike C++), therefore, SomeClass X = Y; is
somewhat pointless, since you can use Y instead in the body of the
function. However, as I type this I realize that if you pass by value
rather than by reference, that is, if you do NOT use the 'ref'
keyword: void somemethod(ref SomeClass Y) // i.e. DON'T do this, but
rather, as in your example, void somemethod(SomeClass Y), then you can
use SomeClass X = Y; for code clarity, if you do stuff in the method
that returns SomeClass (to give a reader of your code some idea that
you're not returning Y as modified, but X, though one can argue that
the lack of the 'ref' keyword should clue in the reader that this is
the case.

Come to think of it, now that I see C# is like old-fashioned C in that
you are keeping track of passing a pointer (or reference) around, why
would you ever use SomeClass X = Y; ? Just use Y directly in your
method. All this time in C# I've been doing either "FIRST WAY" or,
(rarely, but in a few instances) "SECOND WAY" above, and this has
needlessly been somewhat slowing down my programs, though how much is
debatable with all the stuff going on behind the scenes in C#.NET
anyway.

RL

.



Relevant Pages

  • The proper way of function encapsulation.
    ... //Assumes the existence of SomeClass. ... void enterNumber(); ... PQueue TestPQueue; ...
    (comp.object)
  • Re: Idea
    ... My goal is to find the best ways to specialize containers wich holds objects. ... //Assumes the existence of SomeClass. ... void enterNumber(); ... case Top: showHeap(); break; ...
    (alt.comp.lang.learn.c-cpp)
  • Re: The proper way of function encapsulation.
    ... > Perhaps that's what bothers me. ... what is the PQueue class? ... to manipulate a priority queue that contains SomeClass objects. ... void enterNumber() { ...
    (comp.object)
  • Re: Idea
    ... > showHeap() as well. ... your PQueue *in terms of* std::priority_queue. ... bool operator<(const SomeClass& l, const SomeClass& r) ... void enterNumber(); ...
    (alt.comp.lang.learn.c-cpp)