Initializing an array of objects that have constructors

Is it possible to initialize an array of objects that have different constructors?
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "Map_Segment.h"

const Map_Segment Maps[] = { Object( "Parameter" ), Object2( "Parameter2" };

int main()
{
    return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>

using namespace std;

class foo
{
  public:
    string name;
    foo(){}
};

class c1 : public foo
{
  public:
    c1(string s) { name = s; }
};

class c2 : public foo
{
  public:
    c2(string s) { name = s; }
};

int main()
{
  foo flist[] = { (c1("c1")), (c2("c2")) };
  cout << "c1.name = " << flist[0].name << endl;
  cout << "c2.name = " << flist[1].name << endl;
  return 0;
}


output is:

1
2
c1.name = c1
c2.name = c2


Looks like in theory works fine.
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "Map_Segment.h"

Map_Segment Maps[] = { (Home("Home")), (Forest("Forest")) };

int main()
{
    return 0;
}

I get an error. Not declared in scope?
Are Home and Forest derived from Map_Segment?
This is the header for the class, I'm not sure what you mean, i'm trying to create objects of type Map_Segment though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef MAP_SEGMENT_H
#define MAP_SEGMENT_H
#include "Map_Tile.h"
#include <iostream>
using std::string;


class Map_Segment
{
    public:
        const static int MAP_SIZE = 10;

        Map_Segment(string areaName);

    private:
        Map_Tile game_board[MAP_SIZE][MAP_SIZE];

        void get_valid_exits(const string& areaName);
        void get_exit_descriptions(const string& areaName);
        void get_area_description(const string& areaName);
};

#endif 
Not declared in scope means that the compiler doesn't know what a Home or Forest are because you haven't declared or defined them previously. In the example I gave above I have a base class "foo" and then 2 derived classes "c1" and "c2". In your case you would make 2 classes "Home" and "Forest" which derive from Map_Segment. Take a look at this maybe: http://www.cplusplus.com/doc/tutorial/polymorphism/
So, something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "Map_Segment.h"

const Map_Segment Forest( "Forest" );
const Map_Segment Home( "Home" );
const Map_Segment Dungeon( "Dungeon" );

const Map_Segment Maps[] = { Forest, Home, Dungeon };

int main()
{
    return 0;
}
Those are instances of Map_Segment not new classes that derive from it, but yes they work just fine as well. The only criteria for storing in Maps array is that all items are of type Map_Segment (which instances certainly are, and derived classes would also be)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <vector>
#include <string>

struct map_segment
{
    map_segment( std::string  ) { /* ... */ } // note: not explicit

    // ...
};

struct map_segment_a
{
    map_segment_a( std::string area_name ) { /* ... */ } // note: not explicit

    // delegating constructor http://www.stroustrup.com/C++11FAQ.html#delegating-ctor
    map_segment_a( const char* area_name ) : map_segment_a( std::string(area_name) ) {} // not explicit

    // ...
};

int main()
{
    const map_segment maps[] { std::string("Forest"), std::string("Home"), std::string("Dungeon") } ;

    const map_segment_a maps_a[] { "Forest", "Home", "Dungeon" } ;
}

http://coliru.stacked-crooked.com/a/350b3f0aa560b76f
Last edited on
Mr D wrote:
So, something like this?
This is not so optimal since you have the objects twice. You can initialize it like so:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "Map_Segment.h"

const Map_Segment Maps[] = { { "Forest" }, { "Home" }, { "Dungeon" } };

int main()
{
    return 0;
}


if you initialize with derived classes all derived features are lost
Thank you Coder777, that worked perfectly!
Topic archived. No new replies allowed.