using & in C++

using & in C++

Post by Stephen Closs » Thu, 22 Nov 2001 01:59:01



Just a quick question.
        Why is C++ do I always seem to see a call to a function done like:

        func( a );

        and handled like this:

        void func( int &a ) { ... }

        Is there a reason people use the '&' in this case? Is this a pass by reference?

        thanks for your help,
        Regards,
                Confused.

 
 
 

using & in C++

Post by Neil Butterwort » Thu, 22 Nov 2001 02:01:16



Quote:> Just a quick question.
> Why is C++ do I always seem to see a call to a function done like:

> func( a );

> and handled like this:

> void func( int &a ) { ... }

> Is there a reason people use the '&' in this case? Is this a pass by

reference?

Yes. They use it for a couple of reasons:

- to avoid any copying overhead
- to allow them to modify the object within the function

This should be covered in any beginners book on C++ - which one are you
using?

NeilB

 
 
 

using & in C++

Post by Luke » Thu, 22 Nov 2001 08:07:41


Using & in the function declaration indeed directs the compiler to pass the
parameter by reference. This means that you can modify the contents of the
original item and not just a local copy as you would if you did not pass by
reference.

Do not confuse it with the other meaning where, i.e. CTester* test = &test2;
which is the "address of" operator telling the compiler to resolve the
statement to an address not the actual object.

Luke


Quote:> Just a quick question.
> Why is C++ do I always seem to see a call to a function done like:

> func( a );

> and handled like this:

> void func( int &a ) { ... }

> Is there a reason people use the '&' in this case? Is this a pass by
reference?

> thanks for your help,
> Regards,
> Confused.

 
 
 

using & in C++

Post by Jon Be » Thu, 22 Nov 2001 08:24:15




>Just a quick question.
>    Why is C++ do I always seem to see a call to a function done like:

>    func( a );

>    and handled like this:

>    void func( int &a ) { ... }

>    Is there a reason people use the '&' in this case? Is this a
>       pass by reference?

Yes, that's exactly what it is.  Doesn't your C++ book say anything about
references?  If it doesn't, you'd better get another one.  ;-)

--

Dept. of Physics and Computer Science        Clinton, South Carolina USA

 
 
 

1. Announce: PluginMaker for Navigator plug-ins using MFC and Visual C++ 4

We are pleased to announce the PluginMaker: software for
creating Netscape Navigator plug-in DLLs using MFC and
Visual C++ version 4.

PluginMaker includes an AppWizard which makes creating
plug-ins as easy as typical Visual C++ AppWizard application
frameworks.  Plug-ins created by PluginMaker will run
under Navigator version 2, yet can take advantage of
newer features when running under Navigator version 3.
16-bit plug-ins can also be created.  Java/JavaScript
interfaces can be created via LiveConnect, and
used when running under v3.

Dialog-based plug-ins are available, making
it simple to put Windows "Controls" in the
plug-ins.  Alternatively, the familiar
document/view model may be used for creating
the plug-ins.

Visual C++ version 4 is required.

For downloading a free evaluation
copy of PluginMaker, please visit
our home page at

  http://www1.usa1.com/~desisoft/

Questions and comments may be addressed

2. NOVELLNP.DLL

3. MFC & Keyboard shortcuts: How to use INS, DEL & ENTER for menu item activation?

4. "CIO-Summit" phishing attempt

5. Announce: PluginMaker for Navigator plug-ins using MFC and Visual C++ 4

6. AmigaDOS parameters...

7. 推荐一个C++论坛 (C++探路者) http://cpp.aosee.com/

8. More AT-Speed Questions

9. Winsock Redirectors & Plug-Ins

10. VC++ child window questions & Ctrl-Ins

11. SDK's for writing MS Word & Internet Explorer add-ins?

12. MS-WORD 6.0 WLL & ODBC DLL Add-Ins

13. Writing MMC Snap-Ins Using Visual Basic?