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. Question about classes

Question about classes

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
7 Posts 2 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.
  • A Offline
    A Offline
    akirilov
    wrote on last edited by
    #1

    Hi guys, I hope my question will be peace of cake for you. Here it is: CmyClass cl; //CmyClass is defined elsewhere. How to define it, so I can create it in a function Init() and destroy it in function FreeAll()? If I declare it in a function it will be created and destroyed within it. If I declare it as a global variable, it will create it in the declaration (wherever this take place in the program). Well, I can use 'CmyClass *cl', but in the code I will have to use '->' instead of '.', which ... makes me angry. Is there another approach?

    C 1 Reply Last reply
    0
    • A akirilov

      Hi guys, I hope my question will be peace of cake for you. Here it is: CmyClass cl; //CmyClass is defined elsewhere. How to define it, so I can create it in a function Init() and destroy it in function FreeAll()? If I declare it in a function it will be created and destroyed within it. If I declare it as a global variable, it will create it in the declaration (wherever this take place in the program). Well, I can use 'CmyClass *cl', but in the code I will have to use '->' instead of '.', which ... makes me angry. Is there another approach?

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Technically (AFAIK) you can't. You may however use - a wrapper of the class providing access to (something similar to lazy initialization or singleton patterns) or - use, whenever needed, a reference as alias to the dereferenced pointer to avoid pointer syntax. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      A 1 Reply Last reply
      0
      • C CPallini

        Technically (AFAIK) you can't. You may however use - a wrapper of the class providing access to (something similar to lazy initialization or singleton patterns) or - use, whenever needed, a reference as alias to the dereferenced pointer to avoid pointer syntax. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        A Offline
        A Offline
        akirilov
        wrote on last edited by
        #3

        use, whenever needed, a reference as alias to the dereferenced pointer to avoid pointer syntax. Could you give me an example? Sorry I'm new in C++ but I'm learning very fast when I see a piece of code.

        C 1 Reply Last reply
        0
        • A akirilov

          use, whenever needed, a reference as alias to the dereferenced pointer to avoid pointer syntax. Could you give me an example? Sorry I'm new in C++ but I'm learning very fast when I see a piece of code.

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          for instance, suppose your class having 2 methods DoSomething, JustDoAnotherThing:

          // _pcl is the global pointer to the CmyClass object.
          // myObj is just an alias to such object.
          CmyClass & myObj = *_pcl;

          // no pointer syntax below
          myObj.DoSomething();
          myObj.JustDoAnotherThing()
          ...

          :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          A 1 Reply Last reply
          0
          • C CPallini

            for instance, suppose your class having 2 methods DoSomething, JustDoAnotherThing:

            // _pcl is the global pointer to the CmyClass object.
            // myObj is just an alias to such object.
            CmyClass & myObj = *_pcl;

            // no pointer syntax below
            myObj.DoSomething();
            myObj.JustDoAnotherThing()
            ...

            :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            A Offline
            A Offline
            akirilov
            wrote on last edited by
            #5

            Thank you very much!!! You saved my day, I really, really hate '->'.

            C 1 Reply Last reply
            0
            • A akirilov

              Thank you very much!!! You saved my day, I really, really hate '->'.

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              wait, friend, wait: you're going to discover some interesting issues using references. :rolleyes: :-D

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              A 1 Reply Last reply
              0
              • C CPallini

                wait, friend, wait: you're going to discover some interesting issues using references. :rolleyes: :-D

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                A Offline
                A Offline
                akirilov
                wrote on last edited by
                #7

                oh, I think I already did ... :(( I tried (global variables): CakRadioBox *prb = NULL; CakRadioBox & rb; well ... it doesn't work, it require initialization I changed it to: CakRadioBox *prb = NULL; CakRadioBox & rb = *prb; so far so good, but when in Init() I wrire: prb = new CakRadioBox(10, 134, 44, 24); rb = *prb I receive exception :rolleyes:

                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