1

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;
}
4
  • 2
    Use std::array. It has natural copying semantics. There's also no reason whatsoever to use new here. Commented Aug 30, 2014 at 21:48
  • @chris Aaannnd that's an answer ;p Commented Aug 30, 2014 at 21:56
  • Does nobody use reference or something? Commented Aug 30, 2014 at 22:11
  • Really thank you! I have figured out myself. One of the easy ways is to use A* z=x->getdata(), passing the data of c to z. Then we can use z to fill b. This might not be a good answer since its efficiency is low. Commented Sep 1, 2014 at 0:23

3 Answers 3

1

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";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I'm a beginner of C++, I will try to use this in future situations.
@EbrahimChu: You're welcome. And don't forget that everyone of us once was a "beginner of C++" ;)
0

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();

1 Comment

Thank you, this is the easiest way I can think.
0

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];

1 Comment

Thank you as well! You all give better solutions than mine!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.