Ebrahim True
Ebrahim True

Reputation: 11

How can I pass an array of objects?

Here I have a very simple program. My aim is to let b equal c, that is to copy all the content of c into b. But I don't know how. The getdata() function returns a pointer pointing to array of objects c, but how can it be used to put c into b?

#include<iostream>
#include<stdlib.h>
using namespace std;
class A
{
    public:
    A(int i,int j):length(i),high(j){}
    int length,high;
};

class B
{
    private:
    A c[3] = {A(9,9),A(9,9),A(9,9)};
    public:
    A* getdata()
    {
        return c;
    }
};

int main()
{
    A b[3]={A(0,0),A(0,0),A(0,0)};
    B *x = new B();
    cout<< x->getdata() <<endl;
    cout << b[1].length<<endl;
    return 0;
}

Upvotes: 1

Views: 73

Answers (3)

Mr.C64
Mr.C64

Reputation: 42934

In modern C++, make yourself a favor and use a convenient container class to store your arrays, like STL std::vector (instead of using raw C-like arrays).

Among other features, std::vector defines an overload of operator=(), which makes it possible to copy a source vector to a destination vector using a simple b=c; syntax.

#include <vector>  // for STL vector
....

std::vector<A> v;  // define a vector of A's

// use vector::push_back() method or .emplace_back()
// or brace init syntax to add content in vector...

std::vector<A> w = v;  // duplicate v's content in w

That's a possible partial modification of your code, using std::vector (live here on codepad):

#include <iostream>
#include <vector>
using namespace std;

class A
{
public:
    A(int l, int h) : length(l), high(h) {}
    int length, high;
};

class B
{
private:
    vector<A> c;

public:
    const vector<A>& getData() const
    {
        return c;
    }

    void setData(const vector<A>& sourceData)
    {
        c = sourceData;
    }
};

int main()
{
    vector<A> data;
    for (int i = 0; i < 3; ++i) // fill with some test data...
        data.push_back(A(i,i));

    B b;
    b.setData(data);

    const vector<A>& x = b.getData();
    for (size_t i = 0; i < x.size(); ++i) // feel free to use range-for with C++11 compilers
        cout << "A(" << x[i].length << ", " << x[i].high << ")\n";
}

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

Here is an example

#include <iostream>
#include <algorithm>

class A
{
public:
    A( int i, int j ) : length( i ), high( j ){}
    int length, high;
};

class B
{
private:
    A c[3] = {A(9,9),A(9,9),A(9,9)};
public:
    A* getdata()
    {
        return c;
    }
};

int main() 
{
    A b[3] = { A(0,0), A(0,0), A(0,0) };

    B *x = new B();
    A *c = x->getdata();

    std::copy( c, c + 3, b );


    for ( const A &a : b ) std::cout << a.length << '\t' << a.high << std::endl;

    delete []x;

    return 0;
}

The output is

9   9
9   9
9   9

Instead of standard algorithm std::copy you may use an ordinary loop. For example

for ( size_t i = 0; i < 3; i++ ) b[i] = c[i];

Upvotes: 0

Arpit
Arpit

Reputation: 775

Instead of creating an array of A i.e. 'b' in main, create a pointer to A. And then initialize it by calling the getdata().

A *b;

B *x = new B();

b = x->getdata();

Upvotes: 0

Related Questions