Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

how to return a struct

I'm having difficutly returning a struct from a function

say I defined my struct as:

struct foo{
int index[8][2];
};

and my function is:

foo a::fxnToDoSomething()
{
      return foo;
}

it complains... what am I doing wrong?

Or maybe you can suggest something better.  What I'm trying to do is return that 8x2 array to another function so that it can examine those values.  That struct is currently in my private section of my class declarations.

thanks
0
CBMLude
Asked:
CBMLude
  • 2
  • 2
  • 2
  • +2
1 Solution
 
AlexFMCommented:
foo a::fxnToDoSomething()
{
      foo f;
      return f;
}
0
 
CBMLudeAuthor Commented:
I get one of these errors:

dice.cpp(50): error C2143: syntax error : missing ';' before 'dice::getTestIndex'

struct testIndex
      {
            int testIndices[8][2];  
      };

testIndex dice::getTestIndex()
{
      testIndex *testPositions = new testIndex();
      return testPositions;
}
0
 
AxterCommented:
You're returing a pointer to a struct.
You need the following return type.

testIndex* dice::getTestIndex()
{
    testIndex *testPositions = new testIndex();
    return testPositions;
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jaime OlivaresCommented:
As an alternative, you can pass it by reference. Don't have to return, just use it directly:

void a::fxnToDoSomething(foo &myStruct)
{
      foo.Member1 = anyvalue;
      foo.Method1(something);
}
0
 
anthony_wCommented:
Or dereference the pointer:

testIndex dice::getTestIndex()
{
     testIndex *testPositions = new testIndex();
     return *testPositions;
}

Be aware that this copies the return value, whereas if you return by pointer, as Axter suggests, then the point refers to the original.
0
 
AxterCommented:
FYI:
The above code has a memory leak.

I don't recommend returning by value.  It's not efficient.
0
 
anthony_wCommented:
> The above code has a memory leak.

Sorry yes, that's what you get for not reading code that you've cut and pasted. I read it as calling a funcion that returned a pointer, rather than creating one with new. Equivalent code for returning by value:

testIndex dice::getTestIndex()
{
     return testIndex();
}

However, I guess in most cases you want to do more than just construct the object.

As for returning by value not being efficient, that depends. In the code above, a good compiler will omit the copy, and just construct the object in the return value. The same optimization can also be done with a named object:

testIndex dice::getTestIndex()
{
     testIndex someVar;
     //do stuff
     return someVar;
}

In any case, I would rather focus on getting code correct rather than guessing what needs to be efficient, and then profile if things are too slow
0
 
Jaime OlivaresCommented:
Have provided a valid alternative
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now