Course list http://www.c-jump.com/bcc/

Pointers and struct


  1. Passing structs to functions
  2. Passing parameters by value
  3. The need for struct pointer
  4. The need for struct pointer, cont.
  5. A named rectangle
  6. A pointer to rectangle name
  7. Updated constructors
  8. Rectangle name setter and getter functions
  9. Using named rectangles
  10. Another twist: dynamic memory allocation
  11. C++ constructor initializer lists
  12. struct Rectangle with dynamic memory allocation
  13. C++ struct destructor
  14. Huge problem discovered
  15. The need for a copy constructor
  16. Another issue with dynamic memory: assignment
  17. The need for overloaded assignment operator
  18. C++ references
  19. The this keyword
  20. Returning *this
  21. The move constructor
  22. Correct implementation for the Rectangle
  23. A better approach: use library string

1. Passing structs to functions


  • 
    struct Rectangle {
        int width;  // member variable
        int height; // member variable
        // Constructors
        Rectangle()
        {
            width = 1;
            height = 1;
        }
        Rectangle( int width_  )
        {
            width = width_;
            height = width_ ;
        }
        Rectangle( int width_ , int height_ )
        {
            width = width_;
            height = height_;
        }
        
        void set_dimensions( int width_ , int height_ )
        {
            width = width_;
            height = height_;
        }
        int area() const
        {
            return ( width * height );
        }
    };//struct Rectangle
    
    
  • 
    #include <iostream>
    void normalize( Rectangle rect )
    {
        rect.set_dimensions( 1, 1 );
    }
    int main()
    {
        Rectangle rect( 333, 222 );
        normalize( rect );
        std::cout << "rectangle width: " << rect.width << '\n';
        std::cout << "rectangle height: " << rect.height << '\n';
        std::cout << "rectangle area: " << rect.area() << '\n';
        return 0;
    }
    
    

2. Passing parameters by value



3. The need for struct pointer



4. The need for struct pointer, cont.



5. A named rectangle



6. A pointer to rectangle name



7. Updated constructors



8. Rectangle name setter and getter functions



9. Using named rectangles



10. Another twist: dynamic memory allocation



11. C++ constructor initializer lists



12. struct Rectangle with dynamic memory allocation



13. C++ struct destructor



14. Huge problem discovered



15. The need for a copy constructor



16. Another issue with dynamic memory: assignment



17. The need for overloaded assignment operator



18. C++ references



19. The this keyword


  • Within a member function, the this keyword is a pointer to the current object.

  • Current object is the object through which the function was called.

  • Majority of member functions never use the this pointer, because its use within the function is implicit:

  • 
    struct Rectangle {
        int width;  // member variable
        int height; // member variable
        char const* ptr_name;
        //...
        void set_name( char const* ptr_name_ )
        {
            // the use of the this keyword
            // is redundant and unnecessary:
            this->ptr_name = ptr_name_;
        }
        char const* get_name() const
        {
            // the use of the this keyword
            // is redundant and unnecessary:
            return this->ptr_name;
        }
        //...
    };//struct Rectangle
    
    

20. Returning *this



21. The move constructor



22. Correct implementation for the Rectangle



23. A better approach: use library string