0

I'm just recently getting into C++ and bashing my head a little bit on the following topic:

I have a Class with 3 double values of which I create the object "supportRefl[4]" as an array.

I now want to return the array in the function "position" of the Class "Scattering" but I can't find a way to that.

It would be greatly appreciated if you can tell me how to do it.

Heres the class:

class Quelle
{
private:
    double distanceq;
    double elevationq;
    double azimuthq;

public:
    void SetValue(double distance, double elevation, double azimuth);
    double Getd(){return distanceq;}
    double Gete(){return elevationq;}
    double Geta(){return azimuthq;}

Quelle(double distance=0, double elevation=0, double azimuth=0); 
//constructor
~Quelle(); //destructor
};

void Quelle::SetValue(double distance, double elevation, double 
azimuth)
{
    distanceq = distance;
    elevationq = elevation;
    azimuthq = azimuth;
}

Quelle::Quelle(double distance, double elevation, double azimuth) 
//constructor
{
}

Quelle::~Quelle() // destructor
{
}

and here the function I would like to return the array back into: I red that you'd have a pointer to the first adress of the array but it didn't work at all.

*double Scattering::position(const double &distance, const double &elevation, const double &azimuth, const double &intensity)
{
    double intensity_position = intensity * 10 * 5;

    m[0] = distance;
    m[1] = elevation;
    m[2] = azimuth;

    double lo[3] = {m[0], m[1]+intensity_position, m[2]-intensity_position}; //zusammenfassen
    double ro[3] = {m[0], m[1]+intensity_position, m[2]+intensity_position};
    double lu[3] = {m[0], m[1]-intensity_position, m[2]-intensity_position};
    double ru[3] = {m[0], m[1]-intensity_position, m[2]+intensity_position};

    //create instances
    Quelle upperLeft(0,0,0);
    Quelle upperRight(0,0,0);
    Quelle lowerLeft(0,0,0);
    Quelle lowerRight(0,0,0);

    const int NUMBER = 4;

    Quelle supportRefl[NUMBER];

    supportRefl[0].SetValue(lo[0],lo[1],lo[2]);
    supportRefl[1].SetValue(ro[0],ro[1],ro[2]);
    supportRefl[2].SetValue(lu[0],lu[1],lu[2]);
    supportRefl[3].SetValue(ru[0],ru[1],ru[2]);

    return supportRefl[];

}

thank you very much for your help, cheers Simon

4
  • 3
    std::array has value semantics and can be returned normally from a function. Try using an std::array instead of C arrays. Commented May 26, 2017 at 19:21
  • Is your class scattering actually named Quelle? Commented May 26, 2017 at 19:27
  • Nevermind your code isn't posted properly for scattering, someone needs to edit it. Commented May 26, 2017 at 19:28
  • You could create a structure that contains the array, and return the structure. Commented May 26, 2017 at 19:28

3 Answers 3

1

Returning raw arrays by value isn't possible with C++. As others suggested, you can use std::array which is a struct that simply wraps a raw array.

For example like this:

typedef std::array<Quelle,4> QuelleX4;

QuelleX4 Scattering::position(const double &distance, const double &elevation, const double &azimuth, const double &intensity)
{
    QuelleX4 supportRefl;
    supportRefl[0].SetValue(lo[0],lo[1],lo[2]);
    supportRefl[1].SetValue(ro[0],ro[1],ro[2]);
    supportRefl[2].SetValue(lu[0],lu[1],lu[2]);
    supportRefl[3].SetValue(ru[0],ru[1],ru[2]);
    return supportRefl;
}

Alternatively, define your own struct or use std::vector, but that allocates on the heap, so it comes at a cost.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much, I didn't had any contact with std::array so far, it compiles just fine. However I'm wondering at the moment how I can read the returned array out in my main. If I'm calling the function like this: int main(int argc, const char * argv[]) { Scattering scattertest = {0,0,0,0}; scattertest.position(100,100,100, 0.1); return 0; } how can I, for example print it out with a simple cout?
0

The problem is that you are trying to return an array that is allocated on the stack, when the function ends this array is deleted, so either you have to create this array on the heap

supportRefl = new Quelle[NUMBER]

and remember to delete it sometime later, it's a bad practice, so you can replace pointer with a smart pointer like unique_ptr or shared_ptr

std::unique_ptr<Quelle[]> supportRefl = make_unique<Quelle[]>(NUMBER)

or create an array somewhere and pass it to the function as an argument.

*double Scattering::position(const double &distance, const double 
&elevation, const double &azimuth, const double &intensity, Quelle * arrayToFill)

or you can return std::vector or std::array

The easiest options are probably std::vector and std::array.

2 Comments

Returning a raw owning pointer from a function pointer to the result is one of the most error prone ways you could do this. You seem to be aware of std::vector so why not formulate an answer with that instead? At the very least, use std::unique_ptr<double[]> instead of double*.
The issue is that C / C++ doesn't allow a function to return an array. As a workaround, C / C++ does allow a function to return a structure (it allocates space from the stack for the structure before calling the function), and the structure could have the array as it's only member.
0
#include <vector>

std::vector<Quelle> Scattering::position(const double &dist, const double &elev, const double &azim, const double &intensity) 
{ 
    double intensity_pos = intensity * 10 * 5;
    double lower_elev = elevation-intensity_pos;
    double upper_elev = elevation+intensity_pos;
    double lower_azim = azim-intensity_pos;
    double upper_azim = azim+intensity_pos;

    //create instances
    std::vector<Quelle> ret_val;

    ret_val.push_back(Quelle(dist,upper_elev,lower_azim)); // upper left
    ret_val.push_back(Quelle(dist,upper_elev,upper_azim)); // upper right
    ret_val.push_back(Quelle(dist,lower_elev,lower_azim)); // lower left
    ret_val.push_back(Quelle(dist,lower_elev,upper_azim)); // lower right

    return ret_val;
}

This could be a static class method.

1 Comment

Instead of ret_val.push_back(Quelle(a,b,c)); I usually prefer to use ret_val.emplace_back(a,b,c);

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.