Jump to content

  • Log In with Google      Sign In   
  • Create Account

array of objects?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 ISDCaptain01   Members   -  Reputation: 1395

Like
0Likes
Like

Posted 17 June 2013 - 07:15 PM

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?

 

 



Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 8880

Like
1Likes
Like

Posted 17 June 2013 - 07:59 PM

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.


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 Kimmi   Members   -  Reputation: 551

Like
0Likes
Like

Posted 18 June 2013 - 12:47 AM

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.


@KimKulling

#4 BitMaster   Crossbones+   -  Reputation: 4088

Like
1Likes
Like

Posted 18 June 2013 - 01:57 AM

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.


Edited by BitMaster, 18 June 2013 - 01:59 AM.


#5 Kimmi   Members   -  Reputation: 551

Like
0Likes
Like

Posted 18 June 2013 - 02:22 AM

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.


@KimKulling

#6 BitMaster   Crossbones+   -  Reputation: 4088

Like
1Likes
Like

Posted 18 June 2013 - 02:28 AM

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.



#7 Kimmi   Members   -  Reputation: 551

Like
0Likes
Like

Posted 18 June 2013 - 02:47 AM

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

 

Kimmi


A complicate solution may indicate a not understood problem.


@KimKulling




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS