Computing.Net > Forums > Programming > C++ returning arrays

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ returning arrays

Reply to Message Icon

Name: david22
Date: March 4, 2004 at 06:22:00 Pacific
OS: WinXP
CPU/Ram: 512
Comment:

I've managed to create an object that holds an array (floats) but I wish to return that value back through a function.

here is the code for the object holding the array.

#include "Object.h"
#include <iostream>
#include <string>
#include "Aplist.h"

using namespace std;

Object::Object(){
}


Object::Object(float prray[3]){
setDetails(array);
}


void Object::setDetails(float array[3]){
for (int i=0; i<4; i++)
testarray[i]=array[i];
}

void Object::Print(){
for (int i=0; i<4; i++)
cout<<testarray[i]<<endl;
}
float Object::getarray(){
return testarray[3];
}


and here is the code that calls the getarray function

void Creator::FindObject(){
float hold[3];
hold[3]=aObject->getarray();
for (int j=0; j<3; j++)
cout<<hold[j];
}

My problem is that when I copy all 4 items of the array back to where the function was called (to store in the hold[3] array), I would assume I need a loop of some sort to return each item back but i'm not sure how

Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: March 4, 2004 at 11:16:50 Pacific
Reply:

I think that an OO friendly solution would be to change getarray() to:

float Object::getarray(int i){
return testarray[i];
}

Then you can use a for loop to process the array contents:

for (int j=0; j<3; j++)
cout<< aObject->getarray(j);
}


The next step would be to have a method that returns the size of the array:

for (int j=0; j<aObject->getarraysize(); ++j) {
cout << aObject->getarray(j);
}


0
Reply to Message Icon

Related Posts

See More


Web Menu link no workin' Active directory and vbs



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ returning arrays

Return arrays? (C++) www.computing.net/answers/programming/return-arrays-c/13375.html

C programming (array) www.computing.net/answers/programming/c-programming-array/6811.html

C++ large array size www.computing.net/answers/programming/c-large-array-size/7963.html