Solved

how to return a struct

Posted on 2004-08-29
11
206 Views
Last Modified: 2010-04-01
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
Comment
Question by:CBMLude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
11 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 50 total points
ID: 11924868
foo a::fxnToDoSomething()
{
      foo f;
      return f;
}
0
 

Author Comment

by:CBMLude
ID: 11924874
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
 
LVL 30

Expert Comment

by:Axter
ID: 11925205
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!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11926015
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
 
LVL 4

Expert Comment

by:anthony_w
ID: 11951523
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
 
LVL 30

Expert Comment

by:Axter
ID: 11951933
FYI:
The above code has a memory leak.

I don't recommend returning by value.  It's not efficient.
0
 
LVL 4

Expert Comment

by:anthony_w
ID: 11961557
> 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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12328014
Have provided a valid alternative
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question