C++, can I overload assignment operator of a point of a class?












2















It is ok to overload assignment operator of a Class A, but not sure if it is allowed to overload assignment operator of pointer type of class A.
is it legitimate question or not...? If yes, how?



For example, I would like to do ref_count++ whenever we have a pointer reference on it below.



class A{
public:
A(){}
~A(){}
int ref_count{0};
}

main()
{
A* a1 = new A(); //line 1
A* a2 = a1; //line 2
}


After execution of line 1, A.ref_count is 1
After execution of line 2, A.ref_count is 2










share|improve this question


















  • 1





    There is no operator=() involved in the two lines in your main()

    – Swordfish
    Nov 13 '18 at 8:44






  • 1





    What you're doing is copy initialization, not assignment.

    – Some programmer dude
    Nov 13 '18 at 8:46













  • And no you can't overload pointer assignment. The assignment operator must be a member function, and when you assign to a pointer it will not be called.

    – Some programmer dude
    Nov 13 '18 at 8:47






  • 3





    Fortunately, you don't need to implement ref counting for pointers yourself, in C++ they are spelled std::shared_ptr<A>.

    – StoryTeller
    Nov 13 '18 at 8:47











  • If you want to add more custom code other than just ref-counting, you can write your own wrapper class and overload operator* and operator-> for access

    – dave
    Nov 13 '18 at 8:49
















2















It is ok to overload assignment operator of a Class A, but not sure if it is allowed to overload assignment operator of pointer type of class A.
is it legitimate question or not...? If yes, how?



For example, I would like to do ref_count++ whenever we have a pointer reference on it below.



class A{
public:
A(){}
~A(){}
int ref_count{0};
}

main()
{
A* a1 = new A(); //line 1
A* a2 = a1; //line 2
}


After execution of line 1, A.ref_count is 1
After execution of line 2, A.ref_count is 2










share|improve this question


















  • 1





    There is no operator=() involved in the two lines in your main()

    – Swordfish
    Nov 13 '18 at 8:44






  • 1





    What you're doing is copy initialization, not assignment.

    – Some programmer dude
    Nov 13 '18 at 8:46













  • And no you can't overload pointer assignment. The assignment operator must be a member function, and when you assign to a pointer it will not be called.

    – Some programmer dude
    Nov 13 '18 at 8:47






  • 3





    Fortunately, you don't need to implement ref counting for pointers yourself, in C++ they are spelled std::shared_ptr<A>.

    – StoryTeller
    Nov 13 '18 at 8:47











  • If you want to add more custom code other than just ref-counting, you can write your own wrapper class and overload operator* and operator-> for access

    – dave
    Nov 13 '18 at 8:49














2












2








2








It is ok to overload assignment operator of a Class A, but not sure if it is allowed to overload assignment operator of pointer type of class A.
is it legitimate question or not...? If yes, how?



For example, I would like to do ref_count++ whenever we have a pointer reference on it below.



class A{
public:
A(){}
~A(){}
int ref_count{0};
}

main()
{
A* a1 = new A(); //line 1
A* a2 = a1; //line 2
}


After execution of line 1, A.ref_count is 1
After execution of line 2, A.ref_count is 2










share|improve this question














It is ok to overload assignment operator of a Class A, but not sure if it is allowed to overload assignment operator of pointer type of class A.
is it legitimate question or not...? If yes, how?



For example, I would like to do ref_count++ whenever we have a pointer reference on it below.



class A{
public:
A(){}
~A(){}
int ref_count{0};
}

main()
{
A* a1 = new A(); //line 1
A* a2 = a1; //line 2
}


After execution of line 1, A.ref_count is 1
After execution of line 2, A.ref_count is 2







c++ pointers operator-overloading






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 8:42









Kelvin LaiKelvin Lai

344




344








  • 1





    There is no operator=() involved in the two lines in your main()

    – Swordfish
    Nov 13 '18 at 8:44






  • 1





    What you're doing is copy initialization, not assignment.

    – Some programmer dude
    Nov 13 '18 at 8:46













  • And no you can't overload pointer assignment. The assignment operator must be a member function, and when you assign to a pointer it will not be called.

    – Some programmer dude
    Nov 13 '18 at 8:47






  • 3





    Fortunately, you don't need to implement ref counting for pointers yourself, in C++ they are spelled std::shared_ptr<A>.

    – StoryTeller
    Nov 13 '18 at 8:47











  • If you want to add more custom code other than just ref-counting, you can write your own wrapper class and overload operator* and operator-> for access

    – dave
    Nov 13 '18 at 8:49














  • 1





    There is no operator=() involved in the two lines in your main()

    – Swordfish
    Nov 13 '18 at 8:44






  • 1





    What you're doing is copy initialization, not assignment.

    – Some programmer dude
    Nov 13 '18 at 8:46













  • And no you can't overload pointer assignment. The assignment operator must be a member function, and when you assign to a pointer it will not be called.

    – Some programmer dude
    Nov 13 '18 at 8:47






  • 3





    Fortunately, you don't need to implement ref counting for pointers yourself, in C++ they are spelled std::shared_ptr<A>.

    – StoryTeller
    Nov 13 '18 at 8:47











  • If you want to add more custom code other than just ref-counting, you can write your own wrapper class and overload operator* and operator-> for access

    – dave
    Nov 13 '18 at 8:49








1




1





There is no operator=() involved in the two lines in your main()

– Swordfish
Nov 13 '18 at 8:44





There is no operator=() involved in the two lines in your main()

– Swordfish
Nov 13 '18 at 8:44




1




1





What you're doing is copy initialization, not assignment.

– Some programmer dude
Nov 13 '18 at 8:46







What you're doing is copy initialization, not assignment.

– Some programmer dude
Nov 13 '18 at 8:46















And no you can't overload pointer assignment. The assignment operator must be a member function, and when you assign to a pointer it will not be called.

– Some programmer dude
Nov 13 '18 at 8:47





And no you can't overload pointer assignment. The assignment operator must be a member function, and when you assign to a pointer it will not be called.

– Some programmer dude
Nov 13 '18 at 8:47




3




3





Fortunately, you don't need to implement ref counting for pointers yourself, in C++ they are spelled std::shared_ptr<A>.

– StoryTeller
Nov 13 '18 at 8:47





Fortunately, you don't need to implement ref counting for pointers yourself, in C++ they are spelled std::shared_ptr<A>.

– StoryTeller
Nov 13 '18 at 8:47













If you want to add more custom code other than just ref-counting, you can write your own wrapper class and overload operator* and operator-> for access

– dave
Nov 13 '18 at 8:49





If you want to add more custom code other than just ref-counting, you can write your own wrapper class and overload operator* and operator-> for access

– dave
Nov 13 '18 at 8:49












2 Answers
2






active

oldest

votes


















1














I believe you mean pointer assignment. Short answer: no, you can't.






share|improve this answer































    2














    It looks like you wan't a reference counted pointer. First lets examine why your solution doesn't work:




    1. Here: A* a1 = new A(); you are doing pointer assignment, not class assignment. You cannot overload this operation.

    2. In your definition of Class A you implement reference counting. This is not a good solution, what if you want a new class (Class B) that also requires reference counting? You have to implement it all over again. This is not nice or re-usable.


    So, what can we do? Well, it depends on why you wan't reference counting. It seems like you wan't self managed memory. Fortunatley, c++ provides a construct for this already. It is std::shared_ptr. You can use it like this:



    {
    std::shared_ptr<A> a1 = std::make_shared<A>(); // Reference count is set at 1
    {
    std::shared_ptr<A> a2 = a1; // Reference count is incremented to 2
    } // Reference count is decremented to 1
    } // Reference count is decremented to 0 and the memory is released.





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276960%2fc-can-i-overload-assignment-operator-of-a-point-of-a-class%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      I believe you mean pointer assignment. Short answer: no, you can't.






      share|improve this answer




























        1














        I believe you mean pointer assignment. Short answer: no, you can't.






        share|improve this answer


























          1












          1








          1







          I believe you mean pointer assignment. Short answer: no, you can't.






          share|improve this answer













          I believe you mean pointer assignment. Short answer: no, you can't.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 8:49









          darunedarune

          1,134516




          1,134516

























              2














              It looks like you wan't a reference counted pointer. First lets examine why your solution doesn't work:




              1. Here: A* a1 = new A(); you are doing pointer assignment, not class assignment. You cannot overload this operation.

              2. In your definition of Class A you implement reference counting. This is not a good solution, what if you want a new class (Class B) that also requires reference counting? You have to implement it all over again. This is not nice or re-usable.


              So, what can we do? Well, it depends on why you wan't reference counting. It seems like you wan't self managed memory. Fortunatley, c++ provides a construct for this already. It is std::shared_ptr. You can use it like this:



              {
              std::shared_ptr<A> a1 = std::make_shared<A>(); // Reference count is set at 1
              {
              std::shared_ptr<A> a2 = a1; // Reference count is incremented to 2
              } // Reference count is decremented to 1
              } // Reference count is decremented to 0 and the memory is released.





              share|improve this answer




























                2














                It looks like you wan't a reference counted pointer. First lets examine why your solution doesn't work:




                1. Here: A* a1 = new A(); you are doing pointer assignment, not class assignment. You cannot overload this operation.

                2. In your definition of Class A you implement reference counting. This is not a good solution, what if you want a new class (Class B) that also requires reference counting? You have to implement it all over again. This is not nice or re-usable.


                So, what can we do? Well, it depends on why you wan't reference counting. It seems like you wan't self managed memory. Fortunatley, c++ provides a construct for this already. It is std::shared_ptr. You can use it like this:



                {
                std::shared_ptr<A> a1 = std::make_shared<A>(); // Reference count is set at 1
                {
                std::shared_ptr<A> a2 = a1; // Reference count is incremented to 2
                } // Reference count is decremented to 1
                } // Reference count is decremented to 0 and the memory is released.





                share|improve this answer


























                  2












                  2








                  2







                  It looks like you wan't a reference counted pointer. First lets examine why your solution doesn't work:




                  1. Here: A* a1 = new A(); you are doing pointer assignment, not class assignment. You cannot overload this operation.

                  2. In your definition of Class A you implement reference counting. This is not a good solution, what if you want a new class (Class B) that also requires reference counting? You have to implement it all over again. This is not nice or re-usable.


                  So, what can we do? Well, it depends on why you wan't reference counting. It seems like you wan't self managed memory. Fortunatley, c++ provides a construct for this already. It is std::shared_ptr. You can use it like this:



                  {
                  std::shared_ptr<A> a1 = std::make_shared<A>(); // Reference count is set at 1
                  {
                  std::shared_ptr<A> a2 = a1; // Reference count is incremented to 2
                  } // Reference count is decremented to 1
                  } // Reference count is decremented to 0 and the memory is released.





                  share|improve this answer













                  It looks like you wan't a reference counted pointer. First lets examine why your solution doesn't work:




                  1. Here: A* a1 = new A(); you are doing pointer assignment, not class assignment. You cannot overload this operation.

                  2. In your definition of Class A you implement reference counting. This is not a good solution, what if you want a new class (Class B) that also requires reference counting? You have to implement it all over again. This is not nice or re-usable.


                  So, what can we do? Well, it depends on why you wan't reference counting. It seems like you wan't self managed memory. Fortunatley, c++ provides a construct for this already. It is std::shared_ptr. You can use it like this:



                  {
                  std::shared_ptr<A> a1 = std::make_shared<A>(); // Reference count is set at 1
                  {
                  std::shared_ptr<A> a2 = a1; // Reference count is incremented to 2
                  } // Reference count is decremented to 1
                  } // Reference count is decremented to 0 and the memory is released.






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 9:20









                  Fantastic Mr FoxFantastic Mr Fox

                  18.5k1867130




                  18.5k1867130






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276960%2fc-can-i-overload-assignment-operator-of-a-point-of-a-class%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Hercules Kyvelos

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud