C++ question
-
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; };
:)
-
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; };
:)
-
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? }
:)
-
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? }
:)
-
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.
:)
-
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
:)
-
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; };
:)
you can't create a CElement simply with
{2, 4}
for instance, because it is not astruct
... moreover, the membersm_na
andm_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! ]
-
you can't create a CElement simply with
{2, 4}
for instance, because it is not astruct
... moreover, the membersm_na
andm_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! ]
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. :)))
:)
-
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. :)))
:)
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