0

I want the class OUT to hold an array of IN pointers. How many is unknown. This is what I have so far.

class OUT{
    class IN{/**/};
    IN** IN_handle;
    int m_size_in;
    OUT(int size_in):m_size_in(size_in){
        IN_handle = new *IN[size_in];
    }
    ~OUT(){
        for(int i=0; i<m_size_in; i++){
            delete IN_handle[i];
        }
        delete IN_handle;
    }
};

compiler says:

cannot convert 'int**' to 'OUT::IN**' in assignment

5
  • 3
    use a std::vector<IN*> Commented Mar 14, 2016 at 13:36
  • 1
    if you do so, you dont have to take care about the memory management. But you still would have to delete the INs that the elements point to (at least thats what you do with the current code). Commented Mar 14, 2016 at 13:38
  • 6
    Generally speaking: Don't use arrays, dynamic memory allocation with naked pointers, and other such C structures if you don't absolutely have to. C++ is so nice with <vector>, <string>, and smart pointers that way... Commented Mar 14, 2016 at 13:38
  • 2
    new *IN ?? Did you mean new IN* ? Commented Mar 14, 2016 at 13:38
  • I agree with @DevSolar. Anyway, notice that the c-tor and d-tor are private. So you can't create an instance of this class externally. Commented Mar 14, 2016 at 13:41

2 Answers 2

5

Use std::vector and std::unique_ptr will save you a lot of work :

#include <vector>
#include <memory>

class OUT{
    public:
        class IN{/**/};
        std::vector<std::unique_ptr<IN>> IN_handle;

        void add(std::unique_ptr<IN> &&new_data)
        {
            IN_handle.push_back(std::move(new_data));
        }

        OUT()
        {
        }

        ~OUT()
        {
        }

        // Functions to manipulate IN_handle
};

and to use it:

OUT out;
out.add(std::make_unique<OUT::IN>());

From http://www.cplusplus.com/reference/vector/vector/:

Vectors are sequence containers representing arrays that can change in size.

Also std::unique_ptr take care of deleting allocated memory, so no need to be worry about that point ;)

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

3 Comments

Now, just need to add the creation of the objects (make_unique) and this is a top notch answer.
I would suggest to mention explicitly, that unique_ptr takes care of deleting. Ignorants like me might not be aware
@Garf365 well, thanks for not mentioning it in the first version of the answer. I still believe that looking something up by myself makes a better learning experience than just reading it on SO :P. Seriously, I am still not best friend of smarpointers, and imho this is a really nice example
1

Use a combination of std::vector with std::unique_ptr as your vector elements. Do not manually manage your pointers or dynamic arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.