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. C++ question

C++ question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++data-structures
9 Posts 4 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
    Alex_Y
    wrote on last edited by
    #1

    Because VC8 don't alows to intit array of objects this way: CElement elemArr[]={ {1, 2}, {3, 4}, {5, 6} }; I decided to convert it to that way, but I feel it not relible CElement elemArr[]={ CElement(1, 2), CElement(3, 4), CElement(5, 6) }; Is it possible that elements of array will be self destroyed before elemArr? class CElement { public: CElement(int nA){m_nA = nA;}; CElement(int nA, int nB){m_nA = nA;m_nB = nB;}; virtual ~CElement(){m_nA = 0;m_nB = 1;}; virtual int Method(){m_nA = -1;m_nB = -3;return 0;}; protected: int m_nA; int m_nB; };

    :)

    V T 2 Replies Last reply
    0
    • A Alex_Y

      Because VC8 don't alows to intit array of objects this way: CElement elemArr[]={ {1, 2}, {3, 4}, {5, 6} }; I decided to convert it to that way, but I feel it not relible CElement elemArr[]={ CElement(1, 2), CElement(3, 4), CElement(5, 6) }; Is it possible that elements of array will be self destroyed before elemArr? class CElement { public: CElement(int nA){m_nA = nA;}; CElement(int nA, int nB){m_nA = nA;m_nB = nB;}; virtual ~CElement(){m_nA = 0;m_nB = 1;}; virtual int Method(){m_nA = -1;m_nB = -3;return 0;}; protected: int m_nA; int m_nB; };

      :)

      V Offline
      V Offline
      valikac
      wrote on last edited by
      #2

      definitely a() { throw 0 } try { a b } catch {} Kuphryn

      A 2 Replies Last reply
      0
      • V valikac

        definitely a() { throw 0 } try { a b } catch {} Kuphryn

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

        No I talk only with situation with array, NOT just single object void f(int nI) { CElement elemArr[]={ CElement(1, 2), CElement(3, 4), CElement(5, 6) }; int nJ = nI; nJ++; //Question elements off array could possible be destroyed right after declaration line? //What is the scope/lifetime of my elements? }

        :)

        V 1 Reply Last reply
        0
        • A Alex_Y

          No I talk only with situation with array, NOT just single object void f(int nI) { CElement elemArr[]={ CElement(1, 2), CElement(3, 4), CElement(5, 6) }; int nJ = nI; nJ++; //Question elements off array could possible be destroyed right after declaration line? //What is the scope/lifetime of my elements? }

          :)

          V Offline
          V Offline
          valikac
          wrote on last edited by
          #4

          elements inside array are present Kuphryn

          A 1 Reply Last reply
          0
          • V valikac

            elements inside array are present Kuphryn

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

            Could you explain for how long? Show line where distructor of CElement will be called? void f(int nI) { CElement elemArr[]={ CElement(1, 2), CElement(3, 4), CElement(5, 6) }; //1 int nJ = nI; //2 nJ++; //3 }//4 I have guess it is ither 2 or 4? But I want to know motivated answer.

            :)

            1 Reply Last reply
            0
            • V valikac

              definitely a() { throw 0 } try { a b } catch {} Kuphryn

              A Offline
              A Offline
              Alex_Y
              wrote on last edited by
              #6

              Ok here is answer for myself CElement elemArr[]={ CElement(1, 2), CElement(3, 4), CElement(5, 6) }; void OnBnClickedOk() { elemArr[1].Method(); elemArr[2] = CElement(7, 8); // here memcopy happens //here destructor called of local element } //here dtor of global element called

              :)

              1 Reply Last reply
              0
              • A Alex_Y

                Because VC8 don't alows to intit array of objects this way: CElement elemArr[]={ {1, 2}, {3, 4}, {5, 6} }; I decided to convert it to that way, but I feel it not relible CElement elemArr[]={ CElement(1, 2), CElement(3, 4), CElement(5, 6) }; Is it possible that elements of array will be self destroyed before elemArr? class CElement { public: CElement(int nA){m_nA = nA;}; CElement(int nA, int nB){m_nA = nA;m_nB = nB;}; virtual ~CElement(){m_nA = 0;m_nB = 1;}; virtual int Method(){m_nA = -1;m_nB = -3;return 0;}; protected: int m_nA; int m_nB; };

                :)

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

                you can't create a CElement simply with {2, 4} for instance, because it is not a struct... moreover, the members m_na and m_nb are protected and then, can't be accessed from outside the class.


                TOXCCT >>> GEII power

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

                A 1 Reply Last reply
                0
                • T toxcct

                  you can't create a CElement simply with {2, 4} for instance, because it is not a struct... moreover, the members m_na and m_nb are protected and then, can't be accessed from outside the class.


                  TOXCCT >>> GEII power

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

                  A Offline
                  A Offline
                  Alex_Y
                  wrote on last edited by
                  #8

                  Are you kidding? You are, right? Why not code I posted works? Class, struct you can use either. Only in 2005 agregatable type like: CFoo oFooV[] = {{...},{...}, {...}}; can't be declared if CFoo has contructor, virtual function or base class the rest is OK. And never worry about private or protected members just take "this" pointer and add offset to access to the nesesarry field, and use it. Private, protected is only for compilers and school teachers, they like those restrictions. :)))

                  :)

                  Z 1 Reply Last reply
                  0
                  • A Alex_Y

                    Are you kidding? You are, right? Why not code I posted works? Class, struct you can use either. Only in 2005 agregatable type like: CFoo oFooV[] = {{...},{...}, {...}}; can't be declared if CFoo has contructor, virtual function or base class the rest is OK. And never worry about private or protected members just take "this" pointer and add offset to access to the nesesarry field, and use it. Private, protected is only for compilers and school teachers, they like those restrictions. :)))

                    :)

                    Z Offline
                    Z Offline
                    Zac Howland
                    wrote on last edited by
                    #9

                    Alex_Y wrote:

                    Class, struct you can use either. Only in 2005 agregatable type like: CFoo oFooV[] = {{...},{...}, {...}};

                    It worked on older versions of Microsoft's compiler because they didn't meet the standard for this particular case. You cannot set the values of protected/private members directly like this (there are ways to do it, but if you do them, you will quickly be beaten with a stick in any code-review).

                    Alex_Y wrote:

                    And never worry about private or protected members just take "this" pointer and add offset to access to the nesesarry field, and use it. Private, protected is only for compilers and school teachers, they like those restrictions.

                    I do hope you are joking. If you truly believe this, never apply for any position if you see I'm the hiring manager. To answer your original question, when you initialize the array in this manner: CElement elements[] = { CElement(1, 2), CElement(2, 3), ...}; the elements in the array invoke the copy-assignment operator for each element. Thus, the elements themselves will be destroyed, but the values they hold will be copied into your array. For simple cases like this (where your data is just primitive types), you can get away without creating a copy constructor/copy-assignment operator; however, if you had a pointer to heap memory that your class was controlling, only the pointer value would be copied (and since the data in the original would be cleaned up, the pointer would be looking to a place in memory that was no longer allocated). You should get in the habit of creating copy constructors and copy-assignment operators (even if you mark them as private so they can't be invoked).

                    If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                    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