-1

Can I get some help with explanation of the following code?

#include <iostream>

class Vector {
    private:
        double∗ elem; // pointer to the elements
        int sz;       // the number of elements
    public:
        Vector(int s) :elem{new double[s]}, sz{s} { }
        double& operator[](int i) { return elem[i]; }
        int size() { return sz; }
};

I am trying to brush up my C++ knowledge, but this syntax seems very new to me.

More specifically the code under public.

Biffen
  • 6,249
  • 6
  • 28
  • 36
satin
  • 697
  • 2
  • 7
  • 19
  • @Cyber i believe its to declare a pointer – satin Mar 17 '15 at 17:59
  • 1
    There's a difference between `∗` (U+2217, ASTERISK OPERATOR) and `*` (U+002A, ASTERISK). – Biffen Mar 17 '15 at 18:00
  • 2
    There is too much in this question. We don't know what you're struggling with. Is it the member functions? The constructor's member-initialisation list? The `{}` initialisation syntax? You have to be more clear. – Lightness Races in Orbit Mar 17 '15 at 18:01
  • 2
    For those replying to my comment, I was asking a rhetorical question. I was pointing out that they used the wrong character for a pointer, which should be `*` – Cory Kramer Mar 17 '15 at 18:01
  • Sorry about that, let me be more clear. i am particularly struggling to understand `Vector(int s) :elem{new double[s]}, sz{s} { }` – satin Mar 17 '15 at 18:01
  • What precisely are you finding confusing? Can you make this question more specific. Oh, and I think (nay, hope :) @Cyber is pulling your leg. – kdopen Mar 17 '15 at 18:01
  • 4
    Well, if it's not just the braces in that line, then http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor – chris Mar 17 '15 at 18:03

3 Answers3

4

Maybe it's the new C++11 initialisation-lists that are confusing you, you now can initialise a variable with curly-braces {}. For example:

int i{42};
std::vector<int> v{1, 2, 3, 4};

everything else in your code looks pretty standard pre-C++11

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

Vector(int s) defines a constructor - a special method that is called when object is created.

:elem{new double[s]}, sz{s} is a initializer list - it initializes object's fields. The whole part:

Vector(int s):elem{new double[s]}, sz{s} {}

Works same as

Vector(int s) {    
elem = new double[s];
sz = s;
}

However, initializer lists can be used to initialize constants and references.

  • 4
    Assignment in the constructor does *not* work the same as using the initialization list. The first example **initializes** the members while in the second example the variables will be **deafult initialized** and then **assigned** new values in the ctor. – Felix Glas Mar 17 '15 at 18:09
0
Vector(int s) :elem{new double[s]}, sz{s} { }

It is the constructor. It is required an int parameter for instantiating.

elem{new double[s]}, sz{s} { }

This part describes how to initiate the member variables. An array type of double named elem. emem has "s" length array. sz is set as "s".

double& operator[](int i) { return elem[i]; }

This part is in order for access by element index.

Vector v(1);
return v[0]; // <= return double reference

I will share an example. This is introduced in The C++ Programming Language (4th Edition) written by Bjarne Stroustrup

#include <iostream>

using namespace std;

class Vector{
    public:
        Vector(int s) :elem{ new double[s]}, sz{s} {}
        double& operator[](int i) { return elem[i]; }
        int size() { return sz; }
    private:
        double* elem;
        int sz;
};

double read_and_sum(int s)
{
    Vector v(s);
    for (int i=0; i!=v.size(); ++i)
        cin >> v[i];

    double sum = 0;
    for (int i=0; i!=v.size(); ++i)
        sum += v[i];

    return sum;
}

int main() {
    int sum = read_and_sum(3);
    cout << sum << endl;
}
zono
  • 8,366
  • 21
  • 75
  • 113