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. C / C++ / MFC
  4. difference

difference

Scheduled Pinned Locked Moved C / C++ / MFC
9 Posts 6 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.
  • S Offline
    S Offline
    sarojkumarjena
    wrote on last edited by
    #1

    difference between the copy constructor and assignment operator

    T S P 3 Replies Last reply
    0
    • S sarojkumarjena

      difference between the copy constructor and assignment operator

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      is this supposed to be a question ? i mean, grammaticaly speaking, you missed the punctuation and the fundamentals of "how to well form a sentence"... ;P i think you wanted to say : "What is the difference between copy constructors and assignment operators for a know type ?". well, if yes, the answer is simple. the copy constructor can be called once in an object life (at the beginning), when the operator can be called anytime. technically, they do what you tell them to do, so the object can result in being in the same state after both operations, but all depends on what you coded there...


      TOXCCT >>> GEII power

      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

      S 1 Reply Last reply
      0
      • T toxcct

        is this supposed to be a question ? i mean, grammaticaly speaking, you missed the punctuation and the fundamentals of "how to well form a sentence"... ;P i think you wanted to say : "What is the difference between copy constructors and assignment operators for a know type ?". well, if yes, the answer is simple. the copy constructor can be called once in an object life (at the beginning), when the operator can be called anytime. technically, they do what you tell them to do, so the object can result in being in the same state after both operations, but all depends on what you coded there...


        TOXCCT >>> GEII power

        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

        S Offline
        S Offline
        sarojkumarjena
        wrote on last edited by
        #3

        MY QUESTION IS What the copy constructor is doing we can do the same thing through assignment operator.So what is the advantage of copy constructor class A A a; A b; a=b;

        P T S 3 Replies Last reply
        0
        • S sarojkumarjena

          MY QUESTION IS What the copy constructor is doing we can do the same thing through assignment operator.So what is the advantage of copy constructor class A A a; A b; a=b;

          P Offline
          P Offline
          prasad_som
          wrote on last edited by
          #4

          sarojkumarjena wrote:

          MY QUESTION IS

          Dont use caps while expecting others to help.

          sarojkumarjena wrote:

          What the copy constructor is doing we can do the same thing through assignment operator.So what is the advantage of copy constructor

          copy constructor is called when object is function argument , passed by value. when object is returning by value, and object is initializing using other object. i.e.

          class A
          A a;
          A b=a;

          Prasad Notifier using ATL

          1 Reply Last reply
          0
          • S sarojkumarjena

            MY QUESTION IS What the copy constructor is doing we can do the same thing through assignment operator.So what is the advantage of copy constructor class A A a; A b; a=b;

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

            copy constructors are sometimes called implicitely when using templates containers, or things like this. if you don't provide one, you'll get a compiler error saying that no copy constructor were found for the type xxx... moreover, when passing objects by value to/from functions, the objects are duplicated, thus the copy constructor


            TOXCCT >>> GEII power

            [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

            1 Reply Last reply
            0
            • S sarojkumarjena

              difference between the copy constructor and assignment operator

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #6

              Any constructor, including a copy constructor, builds the object from uninitialized memory. Once the object is "built" all the class's invariants have been established. An assignment operator sets the state of the object but the class invariants have already been established by a constructor.

              Steve

              1 Reply Last reply
              0
              • S sarojkumarjena

                MY QUESTION IS What the copy constructor is doing we can do the same thing through assignment operator.So what is the advantage of copy constructor class A A a; A b; a=b;

                S Offline
                S Offline
                SilentSilent
                wrote on last edited by
                #7

                sarojkumarjena wrote:

                What the copy constructor is doing we can do the same thing through assignment operator.So what is the advantage of copy constructor

                The assignment operator destroys/replaces an existing object. The copy constructor creates an object as copy of another object.

                T 1 Reply Last reply
                0
                • S SilentSilent

                  sarojkumarjena wrote:

                  What the copy constructor is doing we can do the same thing through assignment operator.So what is the advantage of copy constructor

                  The assignment operator destroys/replaces an existing object. The copy constructor creates an object as copy of another object.

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #8

                  SilentSilent wrote:

                  The assignment operator destroys an existing object

                  false in the sense that it doesn't call the object's destructor, but true in the sense that it modifies the object integrity


                  TOXCCT >>> GEII power

                  [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                  1 Reply Last reply
                  0
                  • S sarojkumarjena

                    difference between the copy constructor and assignment operator

                    P Offline
                    P Offline
                    Prakash Nadar
                    wrote on last edited by
                    #9

                    You might what to implement the copy construtor or assignment operator to do a deep copy instead of shallow copy and also if you want to do any specific thing while doing the copy.


                    -Prakash

                    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