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. How make a smart reference expose methods of basetype ?

How make a smart reference expose methods of basetype ?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
8 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.
  • M Offline
    M Offline
    Mr Brainley
    wrote on last edited by
    #1

    I have written a reference class for one of my classes. It implements reference counting for that class and that's pretty much it. I have written a cast operator to the class type it references, but the compiler does not use that operator implicitly, so whenever i want to access the referenced object, i have to do an explicit cast (or call a method like getObject()). How can i prevent that ? Here some code to illustrate my problem :

    class MyClassReference;

    class MyClass
    {
    public:
    void doStuff(); // do that thing you do
    static MyClassReference getInstance(); // get an instance of the class, but wrapped in
    // my custom reference-class

    ~MyClass();
    

    private:
    friend class MyClassReference();

    MyClass();                              //  private, so you have to use getInstance();
    
    void addReference();
    void removeRererence();                 //  if (m\_referenceCounter == 0) delete this;
    
    int m\_referenceCounter;
    

    };

    class MyClassReference
    {
    public:
    MyClassReference(MyClass &object); // call addReference
    // fancy copy-constructors and assignment-operators here
    operator MyClass &(); // cast to contained object, not used implicitly

    ~MyClassReference();                //  call removeReference
    

    };

    This is just the principle, feel free to comment on it. wbr Brainley

    C 1 Reply Last reply
    0
    • M Mr Brainley

      I have written a reference class for one of my classes. It implements reference counting for that class and that's pretty much it. I have written a cast operator to the class type it references, but the compiler does not use that operator implicitly, so whenever i want to access the referenced object, i have to do an explicit cast (or call a method like getObject()). How can i prevent that ? Here some code to illustrate my problem :

      class MyClassReference;

      class MyClass
      {
      public:
      void doStuff(); // do that thing you do
      static MyClassReference getInstance(); // get an instance of the class, but wrapped in
      // my custom reference-class

      ~MyClass();
      

      private:
      friend class MyClassReference();

      MyClass();                              //  private, so you have to use getInstance();
      
      void addReference();
      void removeRererence();                 //  if (m\_referenceCounter == 0) delete this;
      
      int m\_referenceCounter;
      

      };

      class MyClassReference
      {
      public:
      MyClassReference(MyClass &object); // call addReference
      // fancy copy-constructors and assignment-operators here
      operator MyClass &(); // cast to contained object, not used implicitly

      ~MyClassReference();                //  call removeReference
      

      };

      This is just the principle, feel free to comment on it. wbr Brainley

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Your operator needs to return the reference to your class:

      MyClass& operator& ();


      Cédric Moonen Software developer
      Charting control [v1.1]

      M 1 Reply Last reply
      0
      • C Cedric Moonen

        Your operator needs to return the reference to your class:

        MyClass& operator& ();


        Cédric Moonen Software developer
        Charting control [v1.1]

        M Offline
        M Offline
        Mr Brainley
        wrote on last edited by
        #3

        same error : doStuff() is not a member of MyClassReference. -- modified at 6:00 Wednesday 21st February, 2007

        C 1 Reply Last reply
        0
        • M Mr Brainley

          same error : doStuff() is not a member of MyClassReference. -- modified at 6:00 Wednesday 21st February, 2007

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Show the code that is giving the error.


          Cédric Moonen Software developer
          Charting control [v1.1]

          M 1 Reply Last reply
          0
          • C Cedric Moonen

            Show the code that is giving the error.


            Cédric Moonen Software developer
            Charting control [v1.1]

            M Offline
            M Offline
            Mr Brainley
            wrote on last edited by
            #5

            MyClassReference ref = MyClass::getInstance();

            ref.doStuff(); // C2039: 'doStuff' is not a member of 'MyClassReference'

            He does not apply the cast implicitly. The following works :

            ((MyClass&)ref).doStuff();

            The compiler told me though, that i have to declare the cast operator

            operator class CNeighbour &();

            in order for this to work. But he still does not apply the cast-operator implicitly.

            C P 2 Replies Last reply
            0
            • M Mr Brainley

              MyClassReference ref = MyClass::getInstance();

              ref.doStuff(); // C2039: 'doStuff' is not a member of 'MyClassReference'

              He does not apply the cast implicitly. The following works :

              ((MyClass&)ref).doStuff();

              The compiler told me though, that i have to declare the cast operator

              operator class CNeighbour &();

              in order for this to work. But he still does not apply the cast-operator implicitly.

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              Of course, you are not using the operator at all ! So it is logical that it complains. BTW, are you trying to do something similar as a smart pointer (but using references instead) ?


              Cédric Moonen Software developer
              Charting control [v1.1]

              M 1 Reply Last reply
              0
              • M Mr Brainley

                MyClassReference ref = MyClass::getInstance();

                ref.doStuff(); // C2039: 'doStuff' is not a member of 'MyClassReference'

                He does not apply the cast implicitly. The following works :

                ((MyClass&)ref).doStuff();

                The compiler told me though, that i have to declare the cast operator

                operator class CNeighbour &();

                in order for this to work. But he still does not apply the cast-operator implicitly.

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

                Mr.Brainley wrote:

                The compiler told me though, that i have to declare the cast operator

                You need to apply that cast. Overloading operation MyClass doesn't mean that you can call members of MyClass using MyClassReference object. By doing this,you can cast MyClassReference class object for MyClass class.

                Prasad Notifier using ATL | Operator new[],delete[][^]

                1 Reply Last reply
                0
                • C Cedric Moonen

                  Of course, you are not using the operator at all ! So it is logical that it complains. BTW, are you trying to do something similar as a smart pointer (but using references instead) ?


                  Cédric Moonen Software developer
                  Charting control [v1.1]

                  M Offline
                  M Offline
                  Mr Brainley
                  wrote on last edited by
                  #8

                  1. Yes, it's not used. Thought it might be possible to use it implicitly, so the code looks better. 2. Yes. It actually is a smart pointer internally, but i tried to give it reference semantics and syntax. I need this for a synchronization problem. I have a list of references that is accessed from many threads. If one thread retrieves a reference from that list, and the list is cleared while it works on the reference, the reference remains valid until it goes out of scope (if it is a smart reference). This way i can minimize synchronization to the pure access on the list. Maybe it would be better if i just overloaded the '->'-operator. It would have pointer-syntax then, but that would be ok i guess.

                  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