Solved

how to return a struct

Posted on 2004-08-29
11
203 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

860 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