Jimmyt1988
Jimmyt1988

Reputation: 21156

Declaring an array of objects, why do i not need to apply new inside the array?

-------------------Question 1:------------------------

I have a Scene class and I want to make an array of scenes called... well... "scenes":

class Scene
{
public:
    int id;
    string title;
    Image backgroundImage;
    Scene( int id, string title, Image backgroundImage );
};

I declare the scenes array inside my Game class in a game header:

Scene scenes[ 2 ];

I then start pumping it with scenes inside my game.cpp loop de loop:

scenes[ 0 ] = Scene();

How comes I can do the above without having to declare a new Scene? for example:

scenes[ 0 ] = new Scene();

Is it because I did not declare the class Scene as public? does it get created as a static or something? I'm confused Scooby!

--------------------Question 2-----------------------

Is there a better way of passing properties for the scene through to the constructor... For example in javascript you might do this:

var Scene = function( properties )
{
    this.id = properties.id;
    this.string = properties.string;
    this.backgroundImage = properties.backgroundImage;
}

var scenes = [
    new Scene( { 
        id:0, 
        string:"a scene", 
        Image: new Image( ... ) 
    } ),        
    new Scene( { 
        id:1, 
        string:"a scene 1", 
        Image: new Image( ... ) 
    } ),
]

this then becomes self documenting.. d'ya catch my drift brah?

------------Note------------:

I think you have to declare it as new because I did not know that just saying scenes[ 0 ] = Scene() declares a new instance of the object?

Upvotes: 0

Views: 101

Answers (5)

David G
David G

Reputation: 96835

  1. In JavaScript, new creates an object, whereas in C++ it returns a pointer to that newly created object. So doing scenes[0] = Scenes() is correct.

  2. Maybe you can try std::vector:

    #include <vector>
    
    std::vector<Scenes> scenes{
        Scenes{0, "a scene", Image{}},
        Scenes{1, "a scene1", Image{}},
    };
    

Upvotes: 1

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145359

#include <string>
using namespace std;

struct Image {};

class Scene
{
private:
    int id_;
    string title_;
    Image backgroundImage_;
public:
    Scene( int id, string const& title, Image const& backgroundImage )
        : id_( id )
        , title_( title )
        , backgroundImage_( backgroundImage )
    {}
};

#include <iostream>
int main()
{
    Scene scenes[] = {
        Scene( 0, "a scene", Image()  ),
        Scene( 1, "a scene 1", Image()  )
        };
    // Whatever.
}

Upvotes: 1

taocp
taocp

Reputation: 23654

For your first question:

Scene scenes[ 2 ]; /declares an array of Scene objects
scenes[ 0 ] = Scene();
How comes I can do the above without having to declare a new Scene? for example:

scenes[ 0 ] = new Scene();
Is it because I did not declare the class Scene as public? 

new operator creates an object, returns a pointer to the object of class. If you use new, you need to declare the array as an array of pointers to Scene. In your array of objects case, if you do not specify which constructor to call to initialize those objects, it will call the default constructor.

Upvotes: 0

Pete Becker
Pete Becker

Reputation: 76438

Scene scenes[2];

This creates an array of 2 Scene objects. Not pointers to objects. Objects. Each object is initialized with the default constructor.

To create an array with objects initialized by some other constructor, just do it:

Scene scenes[2] = {
  Scene(0, "a scene", Image(...)),
  Scene(1, "a scene 1, Image(...)) };

Upvotes: 1

Kerrek SB
Kerrek SB

Reputation: 477368

You want this:

class Scene
{
    int id;
    string title;
    Image backgroundImage;

public:
    Scene(int id, string const & title, Image const & backgroundImage)
    : id(id)
    , title(title)
    , backgroundImage(backgroundImage)
    { }
};

Scene scenes[2] = { Scene(1, "me", image1), Scene(2, "you", image2) };

With a modern compiler (C++11) you can also write:

Scene scenes[2] = { {1, "me", image1}, {2, "you", image2} };

And you would probably prefer std::array<Scene, 2>.

Upvotes: 0

Related Questions