Hi friends,

One of my class member function is supposed to return an array of integers. Here's the function.

int* MyClass::getallVars()
{
    int* a = new int;
    int b[] =
    {
        GetparentVar1(),
        GetparentVar2(),
        GetchildVar1(),
        GetchildVar2(),
    };

    a=b;

    return a;
}

Is there anyway I can do this without the use of second array "b"?

int* a = new int[4]; // allocate memory for 4 integers
a[0] = GetParentVar1();
a[1] = GetParentVar2();
a[2] = GetParentVar3();
a[3] = GetParentVar4();
return a;

thanks, but I already knew this.
Just wanted to know if there is any shortcut like this as we have in Java

int[] a = new int[]{4,5,6,7,7};

The C++ syntax for memory allocation for array does not allow any shortcut like what's done in Java.

You can always simulate the Javs syntax by creating a function that takes a variable number of arguments and returns an allocated int array with all the values received at their correct position in the array.

I disagree with that approach, because evaluation of functions with variable arguments have the reputation to be slow.

First of all, the code that you posted has several errors. First, the array "b" is not a dynamically allocated array, it is a fixed-size array (deduced from the number of elements between curly braces) that resides on the stack frame of the function. This means that after you return from the function, that array will no longer exist and the pointer that you have provided to the caller will be invalid. Second, the memory that is allocated by "a = new int;" is only big enough for 1 integer value. Third, when you do "a=b;" you are just making the pointer a to point to the same location as b, which is the start of the static array that will disappear when you return from the function. Finally, this function has a memory leak because the memory that is allocated with "a = new int;" is never deleted and can never be (it is unreachable memory).

Most importantly, in C++, it's considered very bad practice to allocate memory inside a function and give away a pointer to that memory as a return value, expecting the caller to know how to delete that memory. This might be OK in Java (because Java's GC holds your hand like a little baby), but that's just an undesirable side-effect of garbage collection (it makes people forget that in any language, garbage collected or not, it's poor coding practice to do this).

The usual solution in C++ either something like this:

void MyClass::getallVars(std::vector<int>& out)
{
  out.push_back(GetparentVar1());
  out.push_back(GetparentVar2());
  out.push_back(GetchildVar1());
  out.push_back(GetchildVar2());
}

Or something like this:

template <typename ForwardIter>
void MyClass::getallVars(ForwardIter start) {
  *start++ = GetparentVar1();
  *start++ = GetparentVar2();
  *start++ = GetchildVar1();
  *start++ = GetchildVar2();
};

Or, if you are really in love with the curly-braces style of initialization lists, then you can use this:

template <typename ForwardIter>
void MyClass::getallVars(ForwardIter start) {
  int b[] =
    {
        GetparentVar1(),
        GetparentVar2(),
        GetchildVar1(),
        GetchildVar2(),
    };
  std::copy(b, b+4, start);
};

And, if you are really stubborn, then you can use this C-style hack that will be contrary to almost every rule of good coding practices in C++ (and will also be dangerous and non-portable code, hence, a "hack"), but it approaches your "Java shortcut":

int* MyClass::getallVars()
{
  return (int*) memcpy(new int[4], 
                       {
                         GetparentVar1(),
                         GetparentVar2(),
                         GetchildVar1(),
                         GetchildVar2()
                       }, 4 * sizeof(int));
}

CAVEAT: The above is extremely dirty, do not approach without a Hazmat suit.

thanks mike... that was very useful :)
i'm sorry bout my very limited knowledge in c++

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.