array of objects?

Started by
5 comments, last by Kimmi 10 years, 10 months ago

I was making a coordinates class:


class Coords

{

   public:

    

        int x;

        int y;


        Coords( int one, int two)

       {


         x = one;

         y = two;

        }

};

Okay, now I want to make a an array of this objects:

Coords coordinates[7];

Now how would I go about initializing the x and y coordinates of these objects using the constructor?

coordinates[0](10, 10); ?

I don't think so.

I cant even use coordinates[0].x = 10; coordinates[0].y = 10; either.

How would I about doing this?

Advertisement

That array needs to allocate memory for each object as soon as it's declared, so when you do:


Coords coordinates[7];

It will allocate space for 7 Coords and initialize them with the default constructor. But here you have no default constructor - you provided your own which takes parameters, so this line should fail to compile as the compiler doesn't know what to initialize them with. If the size of your array is known, you can initialize them like this:


Coords coordinates[7] = { Coords(1, 2), Coords(7, -2), Coords(4, 0), /* ... */ };

If it isn't, then you can't really do it. In this case what you want is an array of pointers to coordinates, like this, which is closer to what you seem to want:


Coords *coordinates[7]; // can be done, since we're just allocating space for pointers

for (int t = 0; t < 7; ++t) coordinates[t] = new Coords(x, y); // !!

// don't forget to delete them when you no longer need them

Or, better yet, use an std::vector. That's what it's for smile.png

TL;DR: classes need to be constructed immediately after allocation. You can't have a "allocated but not created yet" object in C++. But pointers provide a mechanism to achieve the kind of behaviour you are seeking. Also, raw pointers are kind of non-idiomatic in C++. You should be using references to pass objects around, and, if you must, use smart pointers to have better control over your objects. I know it's tempting to do "C with classes" coding but C++ has more features than just classes, it has its own standard library and way of doing things.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

And if you want to use a std::vector please add a copy constructor and the == and = operators.

Greetings,

Kim

A complicate solution may indicate a not understood problem.


[twitter]KimKulling[/twitter]

And if you want to use a std::vector please add a copy constructor and the == and = operators.

Greetings,
Kim


In general, that is bad advise. The class originally posted by the OP does not need an explicit copy constructor or operator =. The compiler will generate one that does a perfectly fine job. Needlessly implementing copy constructors and assignment operators is one of the major sources of errors.
There are largely two types of classes: those that are noncopyable (in this case use something like inheriting from boost::noncopyable or the = delete syntax in C++11) or classes that should be able to copy themselves using the automatically generated functions.
If you find more than a tiny fraction of your classes need an explicit copy constructor and assignment operator you are probably doing something wrong with your class design. There are std::shared_ptr/std::unique_ptr that solve a lot of problems (alternatively their pre-C++11 counterparts in Boost). Similar, more specialized classes similar to these might have to be written by yourself, but these will be a tiny fraction of the classes written.

Having an operator == is nice, but not required for storage in a vector. If searching for elements in a container is a common use case I would rather worry about operator < (or a functor which can take that job) so I can store my elements in an always-sorted container like std::set/std::map or apply std::sort on my vector.

Sure, of course you can take the one generated by the compiler. Special in this case it makes sence because of the fact that the data type just containes POD-types. But if you don't store the data itself I just remembered the hint from "Effective c++" to have them in place. And maybe I have to rethink about this hint next time :-).

Personally I prefer to use a shared pointer, so here my knowledge seems to be a little outdated.

Greetings,

Kim

A complicate solution may indicate a not understood problem.


[twitter]KimKulling[/twitter]

As I already said: if you find having the need to write an explicit copy constructor and and assignment operator you are in nearly all cases in one of two scenarios:

1) you are doing something wrong

2) you are writing a small RAII wrapper similar in task to std::shared_ptr/std::unique_ptr but with a more specialized focus.

The one major problem with writing unnecessary copy constructors and assignment operators happens is this: You have a class/structure containing a bunch of data member and all works well. And then you have to add one. Suddenly you have to remember modifying two functions. If you don't you might end up with an extremely annoying to find bug.

And of course no one likes writing code they don't need to. It costs time to write. It costs time to read it again when you are in the area. It costs time to modify when something changes. It costs time to debug.

Thanks for the infomation, I see your point and do agree.

Kimmi

A complicate solution may indicate a not understood problem.


[twitter]KimKulling[/twitter]

This topic is closed to new replies.

Advertisement