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

5
  • If I understand, it's impossible... so I can write the second constructor Dot::Dot (int dot [], int k) like this? If I use dynamic array, it works? Commented Nov 25, 2014 at 22:28
  • @Laura Why not just use std::vector? The empty array syntax is not legal C++ anyway, at least in the context where you're using it. Commented Nov 25, 2014 at 22:33
  • @PaulMcKenzie you mean, use dynamic array? And how i can initialize it as I can use it in the two contructors? Commented Nov 25, 2014 at 22:37
  • @Laura - See my answer below. Commented Nov 25, 2014 at 22:39
  • @Laura - You should also post your setDot function, as it probably alters the array in some way. Commented Nov 25, 2014 at 22:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 - 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: en.cppreference.com/w/cpp/container/vector
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 See live example here: 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.