Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstring>
- #include <iostream>
- class Array
- {
- public:
- Array()
- : m_data (new int[10] )
- {
- }
- ~Array()
- {
- delete[] m_data;
- }
- Array & operator=( const Array & other)
- {
- memcpy( m_data, other.m_data, sizeof(4)*10 );
- return *this;
- }
- int * m_data;
- };
- class User
- {
- public:
- User()
- {
- }
- ~User()
- {
- }
- User operator=( const User& other )
- {
- if(this == &other)return *this;
- m_f1 = other.m_f1;
- m_f2 = other.m_f2;
- m_array = other.m_array;
- return *this;
- }
- int & getFirstOfArray()
- {
- return m_array.m_data[0];
- }
- private:
- int m_f1;
- double m_f2;
- Array m_array;
- };
- class Test
- {
- public:
- void SetUser( const User & user )
- {
- m_user = user;
- }
- int & getFirstOfArray()
- {
- return m_user.getFirstOfArray();
- }
- private:
- User m_user;
- };
- int main(int argc, char** argv )
- {
- Test test;
- User newUser;
- newUser.getFirstOfArray() = 255;
- std::cout << newUser.getFirstOfArray() << std::endl;
- test.SetUser( newUser );
- std::cout << test.getFirstOfArray() << std::endl; //invalid memory usage, potentially crash!
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement