Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. simple c++ question

simple c++ question

Scheduled Pinned Locked Moved ATL / WTL / STL
questionc++
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Timothy_1982
    wrote on last edited by
    #1

    what's the difference between: int func(classA & test) and int funct(classA * test) with differences i don't mean, the first is by reference and the second one is a pointer. But what i want to know is, what are the advantages / disadvantages of those 2 ways of working to each other? And when should i use the first way, and when the second way? why am i asking this? because sometimes i'm programming i don't know for which one to chose, because they both work for me. thx

    J T 2 Replies Last reply
    0
    • T Timothy_1982

      what's the difference between: int func(classA & test) and int funct(classA * test) with differences i don't mean, the first is by reference and the second one is a pointer. But what i want to know is, what are the advantages / disadvantages of those 2 ways of working to each other? And when should i use the first way, and when the second way? why am i asking this? because sometimes i'm programming i don't know for which one to chose, because they both work for me. thx

      J Offline
      J Offline
      James R Twine
      wrote on last edited by
      #2

      First difference, this is the wrong forum for this post!  But, since people make honest mistakes...    Behind the scenes, there is no difference, at least not in VC++ 6.0.  How references are implemented by a compiler is completely up to the compiler, but VC++ uses pointers.  For example, if you create a function in a DLL that takes a reference, and you call it from a C program and pass in a pointer, it will still work correctly (at least, using the same builds of VC++ 6.0).    As far as usage, references are only available in C++, not in C.  Pointers have to be manually dereferenced in order to modify the item that it points to.  References do this automatically, so the syntax of using them is different.  For example:

      int FuncA( int &iTest )
      {
          iTest = 10;          // Modifies Variable Passed In As "iTest"
          *iTest = 10;         // Wrong - References Do Not Need To Be Dereferenced Like Pointers
          return;
      }
       
      int FuncB( int *piTest )
      {
          *piTest = 10;       // Modifies Variable Pointed To By piTest
          piTest = 10;        // Wrong - Modifies The Location The Pointer Points To
          return;
      }
      

      Think of a reference as an "atomatically-dereferencing" pointer.  Barring IntelliSense or a similar coding aid, when you use references, it becomes easy for someone else to call the function not realizing that their variable may be modified by the function.  When using pointers, they have to take the extra step to pass in the address of the variable, so they have an idea of what is going to happen (assuming non-const here).    Additionally, you can pass in a NULL pointer to a function, to indicate "nothing", but a reference has to refer to a real instance of something.  For example, you can have an int pointer with a value of NULL, which means a pointer to nothing/nowhere, but you cannot have a reference that refers to nothing (barring some sneaking coding tricks).    References also have to be initialized, pointers do not:

      int iValue;     // Bad Habit, But Works
      int *piValue;   // Bad Habit, But Works
      int &riValue;   // Wrong - Will Not Compile At All
      

      There are a lot more details available on pointers and references beyond what I have written here...    Peace! -=- James


      T 1 Reply Last reply
      0
      • J James R Twine

        First difference, this is the wrong forum for this post!  But, since people make honest mistakes...    Behind the scenes, there is no difference, at least not in VC++ 6.0.  How references are implemented by a compiler is completely up to the compiler, but VC++ uses pointers.  For example, if you create a function in a DLL that takes a reference, and you call it from a C program and pass in a pointer, it will still work correctly (at least, using the same builds of VC++ 6.0).    As far as usage, references are only available in C++, not in C.  Pointers have to be manually dereferenced in order to modify the item that it points to.  References do this automatically, so the syntax of using them is different.  For example:

        int FuncA( int &iTest )
        {
            iTest = 10;          // Modifies Variable Passed In As "iTest"
            *iTest = 10;         // Wrong - References Do Not Need To Be Dereferenced Like Pointers
            return;
        }
         
        int FuncB( int *piTest )
        {
            *piTest = 10;       // Modifies Variable Pointed To By piTest
            piTest = 10;        // Wrong - Modifies The Location The Pointer Points To
            return;
        }
        

        Think of a reference as an "atomatically-dereferencing" pointer.  Barring IntelliSense or a similar coding aid, when you use references, it becomes easy for someone else to call the function not realizing that their variable may be modified by the function.  When using pointers, they have to take the extra step to pass in the address of the variable, so they have an idea of what is going to happen (assuming non-const here).    Additionally, you can pass in a NULL pointer to a function, to indicate "nothing", but a reference has to refer to a real instance of something.  For example, you can have an int pointer with a value of NULL, which means a pointer to nothing/nowhere, but you cannot have a reference that refers to nothing (barring some sneaking coding tricks).    References also have to be initialized, pointers do not:

        int iValue;     // Bad Habit, But Works
        int *piValue;   // Bad Habit, But Works
        int &riValue;   // Wrong - Will Not Compile At All
        

        There are a lot more details available on pointers and references beyond what I have written here...    Peace! -=- James


        T Offline
        T Offline
        Timothy_1982
        wrote on last edited by
        #3

        thx for this info ;) sorry about the wrong forum, but didn't know where to post it, i didn't find a c++ forum.

        J 1 Reply Last reply
        0
        • T Timothy_1982

          thx for this info ;) sorry about the wrong forum, but didn't know where to post it, i didn't find a c++ forum.

          J Offline
          J Offline
          James R Twine
          wrote on last edited by
          #4

          The **Visual C++ forum is the place for C++ related stuff.    Peace! -=- James


          If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
          Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
          DeleteFXPFiles & CheckFavorites (Please rate this post!)**

          1 Reply Last reply
          0
          • T Timothy_1982

            what's the difference between: int func(classA & test) and int funct(classA * test) with differences i don't mean, the first is by reference and the second one is a pointer. But what i want to know is, what are the advantages / disadvantages of those 2 ways of working to each other? And when should i use the first way, and when the second way? why am i asking this? because sometimes i'm programming i don't know for which one to chose, because they both work for me. thx

            T Offline
            T Offline
            tlg
            wrote on last edited by
            #5

            hmm... All the C++ boffins will tell you to use the reference version because it's "safer", and that's their justification for just about everything to do with the language ;) Just remember that you can't reassign references and you can with pointers - this might nudge you towards the pointer version. Unless you have a fairly strange function, there is no performance gain from using one in particular. If you want to emulate the powers-that-be and go in the direction the C++ standards commitee have not-so-subtlely been trying to push us in, use the reference version. Be aware of the possiblity of unseen anonymous objects being created when you pass by reference (if you do indeed have to create a temporary const object) - obviously this isn't possible with pointers and prevents this bit of bloat. I go with references cos company's love to see them in your code. To be honest there's not much to gain from it, but try and stick to one in style. It also stops you having to stick &'s everywhere :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups