• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ISDCaptain01

array of objects?

6 posts in this topic

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?

 

 

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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

 

Greetings,

Kim

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0