1

I have this class

class Dot
{
    public:            // Methods

    Dot();                                       // Default Constructor
    Dot (int dot [], int k);                     // Constructor
    ~Dot();                                      // Destructor
    int getDot();                                // Get Function
    void setDot (int dot []);                    // Set Function
    void PrintDot ();                            // Print Dot


    private:          // Attributes

    int m_k;
    int m_dot [];
};

And I want to write default constructor

Dot::Dot(): m_k(2), m_dot[] ({0,0})              // Compilation Error


Dot::Dot (int dot [], int k)
{     
       m_k=k;

       m_dot [k]= dot [k];
}   

but I don't know how to initialize the static array m_dot into the default constructor. It doesn't work ... I can't initialize it like constant because of the second constructor (possible to modify the value k and the array dot there)

Thanks

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Laura
  • 69
  • 1
  • 10

1 Answers1

1

The array you are attempting to use is not a static one, as the number of entries is determined by the k parameter you specified in the constructor. The array is actually dynamic, so you can use what C++ offers, and that is std::vector:

#include <vector>
class Dot
{
    public:            // Methods

        Dot();                                       // Default Constructor
        Dot (int dot [], int k);                     // Constructor
       ~Dot();                                      // Destructor
        int getDot();                                // Get Function
        void setDot (int dot []);                    // Set Function
        void PrintDot ();                            // Print Dot

    private:          // Attributes
        std::vector<int> m_dot;
};

Then the constructors will look like this:

Dot::Dot(): m_dot(2,0) {}
Dot::Dot(int dot[], int k) : m_dot(dot, dot+k) {}

Note that a vector is basically a wrapper for a dynamic array. Also note that m_k is no longer needed, since m_dot.size() tells you the number of entries.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • Thanks !! But I don't understand something (I'm a beginner beginner ) about the second constructor, I'm supposed to initialize m_dot i.e. my vector with different value and different length (ex: (1,0,0) in this case k=3 and dot (1,0,0). Here, I change the length of my vector but not its values, isn't it? I'm wrong? – Laura Nov 25 '14 at 22:49
  • @Laura - No problem, I'll explain. The constructor with two arguments does exactly what your version was attempting to do. That is, you were setting each item of `m_dot` with the value of `dot` at position `k`. What the second constructor does is initializes the vector and does a copy of all the `dot` entries into the vector in one go (the resizing is done automatically). The two arguments to the vector are the starting entry in `dot`, and one past the ending entry in `dot`. Read about `vector` here: http://en.cppreference.com/w/cpp/container/vector – PaulMcKenzie Nov 25 '14 at 22:51
  • I'm sorry but I'm stuck since a few hours ... when I want to create a variable Vect2 (type Dot with my second contructor as you told me), and I want to send it to my function PrintDot (that works with my default variable) , I receive kind of error: request for member 'PrintDot' in 'vect2', which is of non-class type 'Dot(int*, int)', what's wrong with this? I mean, vect2 contains m_dot (array) and m_k (integer) I don't understand... – Laura Nov 26 '14 at 07:26
  • @Laura See live example here: http://ideone.com/lQ012o Forget about the `m_k` member variable. It is not needed since a vector knows its own size. Creating unnecessary variables that denote the number of entries just makes it more likely that a bug may occur. The only time we give the size is on the second constructor, and that is because an array is dumb, so we need to tell the constructor how many elements to copy to the vector we're creating -- but that's it. We can throw away this information once the data is copied to the vector. – PaulMcKenzie Nov 26 '14 at 12:11