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. Strange Initialize..........

Strange Initialize..........

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsquestion
4 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.
  • M Offline
    M Offline
    milestanley
    wrote on last edited by
    #1

    In "Programming Priciples and Practice Using C++". chapter 17.5 destructors

    //a very simplified vector of doubles
    class vector{
    int sz; // the size
    double* elem; //a pointer to the elements
    public:
    vector(int s) //constructor
    :sz(s), //"initialize sz", I have no idea what does it mean that writting?
    elem(new double[s]) //initialize elem, and the same as above
    {
    for(int i=0; i < s;i++) elem[i]=0; //initialize elements
    }
    int size() const {return sz;} //the current size
    };

    C L _ 3 Replies Last reply
    0
    • M milestanley

      In "Programming Priciples and Practice Using C++". chapter 17.5 destructors

      //a very simplified vector of doubles
      class vector{
      int sz; // the size
      double* elem; //a pointer to the elements
      public:
      vector(int s) //constructor
      :sz(s), //"initialize sz", I have no idea what does it mean that writting?
      elem(new double[s]) //initialize elem, and the same as above
      {
      for(int i=0; i < s;i++) elem[i]=0; //initialize elements
      }
      int size() const {return sz;} //the current size
      };

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      //"initialize sz", I have no idea what does it mean that writting? it means "call sz's constructor with the value in s." it's just another way to initialize member variables.

      image processing toolkits | batch image processing

      1 Reply Last reply
      0
      • M milestanley

        In "Programming Priciples and Practice Using C++". chapter 17.5 destructors

        //a very simplified vector of doubles
        class vector{
        int sz; // the size
        double* elem; //a pointer to the elements
        public:
        vector(int s) //constructor
        :sz(s), //"initialize sz", I have no idea what does it mean that writting?
        elem(new double[s]) //initialize elem, and the same as above
        {
        for(int i=0; i < s;i++) elem[i]=0; //initialize elements
        }
        int size() const {return sz;} //the current size
        };

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        forPower wrote:

          vector(int s)       //constructor
          :sz(s),           //"initialize sz", I have no idea what does it mean that writting?
          elem(new double\[s\])       //initialize elem, and the same as above
        

        It means set the variable sz to the value of the constructor parameter s, and set the variable elem to point to a new array of doubles, of length s. It is a shorthand alternative to writing the lines

        sz = s;
        elem = new double[s];

        1 Reply Last reply
        0
        • M milestanley

          In "Programming Priciples and Practice Using C++". chapter 17.5 destructors

          //a very simplified vector of doubles
          class vector{
          int sz; // the size
          double* elem; //a pointer to the elements
          public:
          vector(int s) //constructor
          :sz(s), //"initialize sz", I have no idea what does it mean that writting?
          elem(new double[s]) //initialize elem, and the same as above
          {
          for(int i=0; i < s;i++) elem[i]=0; //initialize elements
          }
          int size() const {return sz;} //the current size
          };

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          A value can be put in a variable by either initialization and assignment. Here is the difference -

          // Initializing a variable with a value.
          double d = 25.4;

          // Assigning a value to a variable.
          double d;
          d = 25.4;

          What you're doing is initializing a class member variable because you cannot do the initialization as I did above inside a class. (You can if the variable is a static const variable.) Read about it here - Initializing C++ Class Members[^]

          «_Superman_» I love work. It gives me something to do between weekends.
          Microsoft MVP (Visual C++)

          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