How on Earth does this work

I recently came across this snippet of code from a third party vendor, and it works!  What I don't understand is how.

      int nSelectionStart=0;
      int nSelectionEnd  =0;
      GetSel(nSelectionStart, nSelectionEnd);

At this point, nSelectionStart and End seem to have the correct values.  This violates the rules of 'C', and though I am new to c++, it seems to violate them also.  Does anyone have any ideas?

Thanks, Rick
rickatseasoftAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
if the function GetSel has the parameters defined as byref ( & ), this works fine (and is NOT agains the rules of C)
Infinity08Commented:
>> (and is NOT agains the rules of C)
because it's not C, but C++ :)
grg99Commented:
Could be two things:

(1)  They snuck in some C++ feature, like pass by reference  ( i.e:  void GetSel( &int Start, &int End  ).

(2)  GetSel might be a macro, something like:   #define GetSel(s,e)  GetSel_Func( &(s),  &(e) )

Look at the #include file to see exactly what they did.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

rickatseasoftAuthor Commented:
angelIII:

if the function GetSel has the parameters defined as byref ( & ), this works fine (and is NOT agains the rules of C)

I hate to sound obtuse, but can you be more specific?  Are you referring to the construct mentioned by grg99?


grg99:

Under the heading of "You see what you expect to see", I have viewed the MSDN documentation

void GetSel(
   int& nStartChar,
   int& nEndChar
) const;

many times, and, even though is clearly says int& nStartChar, I, being a 'C' guy,  saw int *nStartChar.  I am guessing that (&int nStart), and (int &Start) are the same thing.  Is this true?  Beyond attempting ( and succeeding nicely I might add) to confuse old 'C' programmers, what is the purpose of writing a function this way?  Is there a functional difference between void Func(int *ii) and calling the function with Func(&ii), and void Func(int &ii), and calling it with Func(ii)?  I guess I am wondering whether the ANSI committee, or whomever just did something to be cute, or is there some substantive purpose for creating this type of call.

One last question.  What is the significance of const at the end of the above declaration?

BTW, I'll raise the points for this one as it seems to be getting involved.

Thanks, Rick

Infinity08Commented:
>> Are you referring to the construct mentioned by grg99?
Yes, at least grg99's (1)

>> what is the purpose of writing a function this way?
Ease of use, avoiding pointers (and so helping to avoid memory leaks), ... you name it.

>> Is there a functional difference between void Func(int *ii) and calling the function with Func(&ii), and void Func(int &ii), and calling it with Func(ii)?
Basically no, except that with pointers you need to dereference explicitly :

    void Func_p(int *ii) {
      *ii = 1;
    }
   
    void Func_r(int & ii) {
      ii = 1;
    }

And need to pass the address of the value :

    int ii;
    Func_p(&ii);
    Func_r(ii);

This should show you the usefullness of references versus pointers. Although references are of course not always the best choice over pointers ! All depends on what you want to do.

>> What is the significance of const at the end of the above declaration?
A member function of a class is declared const this way if it is guaranteed not to change the object when called. This allows the method to be called from a const object ...
rickatseasoftAuthor Commented:
>>>>>>A member function of a class is declared const this way if it is guaranteed not to change the object when called. This allows the method to be called from a const object ...

Does "Object" in this case refer to the class?  

If so, does this simply mean that it may or may not change the arguments, but may not change any of the members of the class?  

What if class members are sent to the function as arguments, may it change them then?

I guess that this sort of construction (int &nStart) makes the internal function code a little easier to write---you'll see fewer stars.

You refer to "memory leaks."  How does this convention prevent memory leaks?  Isn't it difficult to create a memory leak absent calloc, malloc, new,...?  If you haven't used one of these how would a memory leak develop, and how would this type of function call reduce the probability?  If you have used one of these memory allocation functions/operators, how would this convention reduce the risk of a leak.  I'm not trying to be argumentative, I am just trying to understand your comments.

Thanks again, Rick
Infinity08Commented:
>> Does "Object" in this case refer to the class?  
An object is an instance of a class :

class C :

    class C {
      // ...
    };

object c :

    C c;

const object c :

    const C c;

>> If so, does this simply mean that it may or may not change the arguments, but may not change any of the members of the class?
A method declared const may change the arguments of the method but may not change any member variables of the object. So, yes, that's what it means.

>> What if class members are sent to the function as arguments, may it change them then?
I don't know if the compiler will catch that, but it's a very poor design of your class because you allow direct access to the member variables of your class. The data members of a class should be used by the class only ... any changes to them should happen by calling
Infinity08Commented:
clicked Submit too early ... continued :

... any changes to them should happen by calling a class method. And that class method can't be declared const (as it changes the object state).

>> I guess that this sort of construction (int &nStart) makes the internal function code a little easier to write---you'll see fewer stars.
Indeed, but also the external code, as you don't need to use the address operator (&) explicitly.

>> How does this convention prevent memory leaks?
It doesn't prevent them directly, but it can help ... Each pointer you can replace by a reference is one less pointer to worry about, and one less possible memory leak. It's a small advantage, I know, but it still is an advantage :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.