[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

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
0
rickatseasoft
Asked:
rickatseasoft
2 Solutions
 
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)
0
 
Infinity08Commented:
>> (and is NOT agains the rules of C)
because it's not C, but C++ :)
0
 
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.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

0
 
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 ...
0
 
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
0
 
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
0
 
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 :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now