-1
class Sales_data {  
  public:  
  Sales_data(int i, int j, int k) : x(i), y(j), z(k) {
  }  
  private:  
  int x,y,z;  
};

In the above code(more specifically in Sales_data constructor(recited below)), I don't understand the use of colon and comma separated list.

Sales_data(int i, int j, int k) : x(i), y(j), z(k) {
}  

I have never seen a colon(":") following any function/constructor parameter list. What does this colon mean/signify here ?
Moreover, what is this comma separated list after colon ?

  • 3
    What does your C++ book have to say on the subject? – tadman Mar 29 '18 at 17:06
  • That specific line really have nothing to do with functors. What do ***you*** think it might mean? What can you figure out yourself? What part of that line don't you understand? – Some programmer dude Mar 29 '18 at 17:07
  • Is the portion of code x(x1) an executable statement ? ........................................... Is the function below add_x(int x1) : x(x1) { } a new style of initializing member variables ? If yes, then how could I initialize more member variables(say if my struct had say 3 or 4 member variables) ? Can you tell me the "topics names" to read from book so that these all will become very clear. – NIRBHAY KUMAR PANDEY Mar 29 '18 at 18:47
  • poor naming make always things hard to understand. `x` is quite a bad name both for a parameter, and (mainly) for a member variable. And `whatever` is always a bad name when used for both a parameter and a member variable. Help yourself, don't use duplicate identifiers it they can be used in the same scope. – Gian Paolo Mar 29 '18 at 19:00

2 Answers2

1

You may be confused, because the member variable name (x) is same as the function parameter (also x), which you can always avoid for clarity. Simplified code can look like so.

add_x(int x1) : x(x1)  // a contructor that initializes the member vaiable x to x1
{  
}

Still confused? then you can go for this ( not so optimize though)

add_x(int x1) 
{  
   x = x1;
}
seccpur
  • 4,996
  • 2
  • 13
  • 21
0

This is a constructor

This is not a standard function/method. Each class (struct) can have constructor(s). The constructor has the same name as the class and can optionally take parameters.

struct add_x {
    int x;
    add_x(int x) : x(x) {}   // This is a constructor with one paramter
};

To make it easier to read let us format it better.

struct add_x {
    int x;
    add_x(int x)    // Constructor that takes one argument.
        : x(x)      // This is called the initializer list.
    {}              // This is the body of the constructor.
};

The initializer list allows you to list out the member variables (comma separated) and initialize them before the body of the constructor is executed.

In this case the member x is initialized with the parameter x.

#include <iostream>
int main()
{
    add_x   test(5);
    std::cout << test.x << "\n"; // Should print 5
                                 // Note in my example I have removed the private
                                 // section so I can read the value x.
}
Martin York
  • 257,169
  • 86
  • 333
  • 562