-2

I have a BossClass , and I will have 5 entries of BossClass in future. I need them to be organized as an array (to be passed to other class), what would be the preferred method?

Here is some code for clarifying my question.

class BossClass 
{
    public:
        BossClass(int);
};
class MyClass 
{
    public:
        MyClass(BossClass*[5]);
};
MyClass someFunction()
{
    BossClass* bossClasses[5];                //[1]
    for (int i=0; i<5; i++)
    {
        // After some interactive input......
        bossClasses[i] = new BossClass(i);
    }
    return new MyClass(bossClasses);                //[2]
}

the problem in this code is, I cannot instantiate MyClass at [1] because the default constructor is not defined. I cannot change the BossClass (but i can change MyClass) as a workaround handling the array of data. What should I do here?

edit: corrected for some really logical errors. The compiler now calls for error with the message

error: incompatible types in assignment of ‘BossClass**’ to ‘BossClass* [0]’ at [2]

And I am curious why array is not assignable to a pointer, as a further question.

orb
  • 175
  • 2
  • 13
  • _"I cannot instantiate MyClass at [1] because the default constructor is not defined"_ define one then. – πάντα ῥεῖ Aug 11 '15 at 08:55
  • 2
    Do you want pointers or values? You declare an array of `BossClass` but attempt to fill it with `BossClass*`. – TartanLlama Aug 11 '15 at 08:55
  • 1
    Skip the array and use a `std::vector`. Oh, and skip all the `new`-ing. C++ isn't Java! – Bo Persson Aug 11 '15 at 09:02
  • i am being hit by myth that vector is much slower than native arrays as some part of its data are the address. compare to native arrays, after allocation C++ just need some countable jump to access the next element. any mythbuster? – orb Aug 11 '15 at 09:44

3 Answers3

1

Use standard container std::vector<BossClass> instead of the array.

For example

#include <vector>

//...

class BossClass 
{
    public:
        BossClass(int);
};

class MyClass 
{
    public:
        MyClass( const std::vector<BossClass> & );
};
MyClass * someFunction()
{
    std::vector<BossClass> bossClasses;
    bossClasses.reserve( 5 );

    for (int i = 0; i < 5; i++)
    {
        // After some interactive input......
        bossClasses.emplace_back( i );
    }

    return new MyClass( bossClassese );
}

Also you can add one more constructor in class MyClass

        MyClass( std::vector<BossClass> && );

and write in the function

    return new MyClass( std::move( bossClassese ) );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • but would it causes some performance problem? since memory needed for MyClass is pretty much the same and I think a pre-defined malloc is better than vector which trades speed with ability to resize – orb Aug 11 '15 at 09:21
  • @orb As i pointed out in my answer you can move the vector around among functions. Also As I showed you can use method reserve to reserve enough memory. – Vlad from Moscow Aug 11 '15 at 09:26
1

Perhaps something like this:

class MyClass 
{
public:
    MyClass(std::vector<BossClass>);
};

MyClass someFunction()
{
    std::vector<BossClass>   bossClasses;                //[1]
    for (int i=0; i<5; i++)
    {
        // After some interactive input......
        bossClasses.push_back(BossClass(i));
    }
   return MyClass(bossClasses);
}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

Ok, here's a "working" modified snippet:

class BossClass 
{
    public:
        BossClass() = default;
        BossClass(int) { }
};
class MyClass 
{
    public:
        MyClass(BossClass*[5]) { }
};
MyClass* someFunction()
{
    BossClass* bossClasses[5];
    for (int i=0; i<5; i++)
    {
        bossClasses[i] = new BossClass(i);
    }
    return new MyClass(bossClasses);
}

However, remember that C++ is not Java (nor C#). It does not have garbage collector, hence the above code will not clean up all those resources created with new allocator. Much, much better idea for you is to use smart pointers instead of the raw ones. For further information and a quick "how to", you can check out this amazing answer.

Community
  • 1
  • 1
psliwa
  • 1,094
  • 5
  • 9