How do i pass an array of allocated types into functions?

Hey man, i cant seem to figure out how to pass an array of allocated types.
Right now i have tried to use some polymorphism like this:

character = baseclass
swordman = derived class

1
2
3
4
5
6
7
8
9
10
void dosomething(character* x[])
{
   x[5]->attack1();
}

int main();
{
   character* ninjas = new swordman[10];
   dosomething(&ninjas);
}


I get a strange exception. But if i write " x[0]->attack1(); " in the dosomething function, then it works.

So what have i done wrong?!?! ive tried alot of different methods :S

Thanks in advance
Compared with that how the pointer was created your function declaration is invalid (more precisely its declaration is surplus). It should be declared either as

void dosomething(character* x);

or as

void dosomething(character x[]);

or even as

void dosomething(character x[10]);

All three declarations declare the same function. So you even can place all these three declaraions in your code provided that there will be only one its definition.:)

As for your function declaration then you have to write in the function body

x[0][5].attack1();

Or as

( x[0] + 5 )->attack1();

or as

( *x )[5].attack1();
Last edited on
hey thanks for helping me, but it doesn't really seem to work out

void dosomething(character x[]);
void dosomething(character x[10]);

these two function declerations dont work because i need it as reference and also because character have pure virtual functions.

and i also tried this decleration as you suggested:
void dosomething(character* x);

but non of these worked:

x[0][5].attack1();
( x[0] + 5 )->attack1();
( *x )[5].attack1();
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>

struct character
{
    virtual ~character() {}
    virtual void attack() { std::cout << "character::attack\n" ; }
};

void do_something( character* cast[], std::size_t n )
{
    for( std::size_t i = 0 ; i < n ; ++i ) cast[i]->attack() ;
}

int main()
{
    struct swordman : character
    {
        virtual void attack() override { std::cout << "swordman::attack\n" ; }
    };

    std::size_t n = 4 ;
    character** ninjas = new character* [10] ;
    for( std::size_t i = 0 ; i < n ; ++i )
        ninjas[i] = i%2 == 0 ? new swordman : new character ;

    do_something( ninjas, n ) ;

    for( std::size_t i = 0 ; i < n ; ++i ) delete ninjas[i] ;
    delete[] ninjas ;
    ninjas = nullptr ;
}


Do yourself a favour: use a std::vector<> of smart pointers instead.
@Assassinbeast
and i also tried this decleration as you suggested:
void dosomething(character* x);

but non of these worked:

x[0][5].attack1();
( x[0] + 5 )->attack1();
( *x )[5].attack1();


These code examples

x[0][5].attack1();
( x[0] + 5 )->attack1();
( *x )[5].attack1();

demonstrate how you could call the function if it is declared as you showed in the first your post. There is no need to use these statements if you will declare the function as I pointed out.
Topic archived. No new replies allowed.