Hi,

What I'm attempting to do is send data from one class, to another and then store it in that second class as an array.

E.g.
file class1.cpp

class1{
Class2 c;
c.methodAdd(Variable1, id);
cout << c.methodGet(2);
}

file class2.h

class2{
typedef string StringArrayPtr;
StringArrayPtr *array = new StringArrayPtr[3]; //create the array
void methodAdd( string var1, int id){
array[id] = var1;
}

string methodGet(int id){
return array[id]
}
}

This isnt the actual code im using (i have a lot more bits in-between - but thats the general jist of what I need. I've been puzzling over it for the past 4 hours :S

Basically, an external class method which adds data to an array, and then can retrieve the values from it.

Any ideas??

Thanks in advance!!
Mike

Recommended Answers

All 5 Replies

That's some dangerous looking code.

class B
{
    private:
                vector<string> array;
    public:
                void method_add(int &stuff, int more);
};

void B::method_add(int &stuff, int more)
{
    stuff += more;
}
class A
{
    private:
                B objectB;
    public:
                void  do_something(void);
};

void A::do_something(void)
{
    int a = 7, b = 9;

    objectB.method_add(a, b);
    cout << "A.do_something().a: " << a << endl;
}

I would suggest you look for some more class tutorials.

The code I gave was just a very very crude (and bad) example, it isn't what I have at the moment.

I was just getting very confused with sending/receiving data from an array between classes. I can constuct proper classes etc (just didn't have time in that example), but I was wondering how the methods would work (i.e. to put data in an array from class A --> class B, and then retrieve data from a specific index from the array.

Any ideas on that front?

Thanks :)

Just using the string vector as an example.

void B::push_back(string &str)
{
    array.push_back(str);
}

string B::get_string(int element)
{
    return(array[element]);
}

If you use a pointer it should be private(imo), allocated in the constructor, deallocated in the destructor, no direct-access. Just my paranoid way.

hmmm ok, Thanks for your opinion :)

All sorted, thanks for everyones help!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.