memory and passing pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan Liu

    #1

    memory and passing pointers

    Hi,

    I'd like to ask if passing an object as an pointer into a function
    evokes the copy constructor.

    Ivan

  • Ron Natalie

    #2
    Re: memory and passing pointers

    Ivan Liu wrote:
    I'd like to ask if passing an object as an pointer into a function
    evokes the copy constructor.
    No. Copying pointers has no bearing on the object that they point to.

    Comment

    • Ivan Liu

      #3
      Re: memory and passing pointers

      Thanks for the reply.

      So the efficiency is as good as passing a reference? If so why pass
      reference?


      Ron Natalie wrote:
      Ivan Liu wrote:
      >
      I'd like to ask if passing an object as an pointer into a function
      evokes the copy constructor.
      >
      No. Copying pointers has no bearing on the object that they point to.

      Comment

      • Ron Natalie

        #4
        Re: memory and passing pointers

        Ivan Liu wrote:
        Thanks for the reply.
        >
        So the efficiency is as good as passing a reference? If so why pass
        reference?
        >
        >
        You are asking extremely vague questions. The main reasons references
        exist are to accomodate overloading. If it were not for this C++
        could have gotten by without them.

        As for stylistically, it would depend on the situation. Nothing
        is gained by switching from pointers to references blindly.

        Comment

        • benben

          #5
          Re: memory and passing pointers

          >>I'd like to ask if passing an object as an pointer into a function
          >>evokes the copy constructor.
          >No. Copying pointers has no bearing on the object that they point
          >to.
          >
          >
          So the efficiency is as good as passing a reference? If so why pass
          reference?
          >
          Readability I'd say, although it is disputable. Some people prefer
          passing pointers and some prefer passing reference. It's up to you to
          chose your own style.

          One place you really want to pass references instead of pointers is
          overloaded operators.

          Regards,
          Ben

          Comment

          • Jim Langston

            #6
            Re: memory and passing pointers

            "Ivan Liu" <darknails@gmai l.comwrote in message
            news:1159532634 .083104.234720@ b28g2000cwb.goo glegroups.com.. .
            Ron Natalie wrote:
            >Ivan Liu wrote:
            >>
            I'd like to ask if passing an object as an pointer into a function
            evokes the copy constructor.
            >>
            >No. Copying pointers has no bearing on the object that they point to.
            Thanks for the reply.
            >
            So the efficiency is as good as passing a reference? If so why pass
            reference?
            Please don't top post in this newsgroup. Message rearranged.

            The main reason I pass references into functions is so I can use . instead
            of ->


            Comment

            • Gavin Deane

              #7
              Re: memory and passing pointers

              Please don't top-post. Thank you. Rearranged.

              Ivan Liu wrote:
              Ron Natalie wrote:
              Ivan Liu wrote:
              I'd like to ask if passing an object as an pointer into a function
              evokes the copy constructor.
              No. Copying pointers has no bearing on the object that they point to.
              Thanks for the reply.
              >
              So the efficiency is as good as passing a reference? If so why pass
              reference?
              There is a fundamental difference between passing references to objects
              and passing pointers to objects. A reference must always refer to a
              valid object. A pointer may refer to a valid object or may be null.
              Therefore, if a function accepts a pointer, it must accept the
              possibility of a null pointer and check the pointer value before using
              it. Change the function to accept a reference and this extra
              responsibility is removed. The function can safely assume that an
              object is there.

              The only time this is not an advantage is, of course, when, in your
              design, the possibility of an object not existing makes sense. A
              reference is no use here because it always referes to a valid object.
              Pass a pointer and the function can query that pointer to see if it is
              null or not.

              A reference says: Here is the object for you to work with.
              A pointer says: Here is an indicator that tells you whether the
              optional object exists and, if so, where it is.

              Summary: use references when you can and pointers when you must.

              Gavin Deane

              Comment

              • Gianni Mariani

                #8
                Re: memory and passing pointers

                benben wrote:
                >>I'd like to ask if passing an object as an pointer into a function
                >>evokes the copy constructor.
                >No. Copying pointers has no bearing on the object that they point
                >to.
                >
                >>
                >So the efficiency is as good as passing a reference? If so why pass
                >reference?
                >>
                >
                Readability I'd say, although it is disputable. Some people prefer
                passing pointers and some prefer passing reference. It's up to you to
                chose your own style.
                >
                One place you really want to pass references instead of pointers is
                overloaded operators.

                Two things about references that are not supported by pointers.

                a) References can't be changed after being initialized
                b) The lifetime of a reference can be transferred to objects.

                (from an earlier post of mine)

                #include <iostream>
                #include <ostream>

                struct A
                {
                A()
                {
                std::cout << "A default\n";
                }

                A( const A & )
                {
                std::cout << "A copy\n";
                }

                A & operator= ( const A & )
                {
                std::cout << "A operator =\n";
                return * this;
                }

                ~A()
                {
                std::cout << "A Destruct\n";
                }

                };

                int main()
                {
                {
                const A & a = A();

                std::cout << "Temporary lives !\n";

                A y( a );
                }

                std::cout << "Temporary is gone !\n";

                {
                const A * a = & A();

                std::cout << "Temporary is gone already - a dangles!\n";
                }

                }

                Comment

                • Ron Natalie

                  #9
                  Re: memory and passing pointers

                  Gavin Deane wrote:
                  A pointer may refer to a valid object or may be null.
                  Therefore, if a function accepts a pointer, it must accept the
                  possibility of a null pointer and check the pointer value before using
                  it.
                  That's certainly not true. The C++ standard library itself is
                  full of functions that assume (they are not required to check)
                  that the caller is not passing a NULL or otherwise invalid pointer.

                  >

                  Comment

                  Working...