initialization list ??

I am reading Data Structures and Algorithms with Object-Oriented Design Patterns in C++

Here is talking about dynamic array, class code below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
template <class T>
class Array
{
protected:
    T* data;
    unsigned int base;
    unsigned int length;
public:
    Array ();
    Array (unsigned int, unsigned int = 0);
    ~Array ();

    Array (Array const&);
    Array& operator = (Array const&);

    T const& operator [] (unsigned int) const;
    T& operator [] (unsigned int);

    T const* Data () const;
    unsigned int Base () const;
    unsigned int Length () const;

    void SetBase (unsigned int);
    void SetLength (unsigned int);
};



Here is the constructor

1
2
3
4
5
6
template <class T>
Array<T>::Array () :
    data (new T [0]),
    base (0),
    length (0)
    {}



Because I haven't seen coding style like this, I am wondering it is same as

1
2
3
4
5
6
7
template <class T>
Array<T>::Array ()
{
    data = new T[0];
    base = 0;
    length = 0;
}


Thanks for answers.
Last edited on
As far as i know, it is the same. It just different way to code it, that all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// example using string
#include <iostream>
#include <string>
using namespace std;

int main()
{
        string word = "hello world";
	string word2("hello hello");
	cout<< word <<endl;

system("pause");
return 0;
}
Last edited on
What's being used in the first example is the initialization list, which is the preferred method to initialize class members (and in some cases the only possible one).
Though in many cases both have exactly the same result as @Athar mentioned it's the preferred way to initialize your data members.

There are some reasons for that. For example for non build-in objects you actually initialize your object twice or to be more correct you don't initialize twice but you use assignment to give your data members values (they have previously been initialized to default values).

Also in some cases you can only use the 1st method. If you want to initialize a data member of your base class (supposed there is one) you can do it only there.
In addition to that, there are some other cases where you can only use the initialization list.
Those being when there isn't a default constructor for a member, or when you have a constant or a reference as a class member.
Thanks for answering.

I still have some problems.

Accroding to the your suggestions, if I want to practice the code myself, can I change the way it coded in the first method with my own way, or I have to learn the coding way in the book?

Thanks for answers
I have to learn the coding way in the book?

Yes, you have to. Constructor initialization lists aren't something you can just skip over when learning C++ (not that it's complicated - learning all the litte details and facets of how it works should take about two minutes).

if I want to practice the code myself, can I change the way it coded in the first method with my own way

Yes, you can. In this particular example, both variants do the same.
Topic archived. No new replies allowed.