Solved

how to return a struct

Posted on 2004-08-29
11
200 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now