Page 1 of 1

Passing containers to functions

Posted: 14 Dec 2005, 18:56
by krogothe
Say i have a simple but big struct like this (pseudo-codish, ignore syntax errors):

Code: Select all

struct santasbag
{
int toy1;
float toy2;
char toy3;
int reindeer1;
float sleigh;
bool claus;
int....
...and so on...
}
basically it has a lot of information in it.

Now say i have a function that will use one or two items from that struct (eg toy1 and sleigh). I could do it two ways:

Code: Select all

void Xmas::SendPresent(int var1, float var2)
{
x = var1;
y += var1 + var2;
}
santabag  gimmeprezzies;
sendpresent (gimmeprezzies.toy1,gimmeprezzies.sleigh)

Code: Select all

void Xmas::SendPresent(santabag my_bag)
{
x = my_bag.toy1;
y += my_bag.toy1 + sleigh;
}
santabag  gimmeprezzies;
sendpresent (gimmeprezzies)
would it be slower to pass the whole struct like shown in the second codebox? Its easier to use and simpler but i have some speed-critical functions and i wouldnt want to use that if it made the function run slower by loading unnecessary data.

Also, in case it IS slower, would it be too bad if santasbag only had say, 6-8 items?

Posted: 14 Dec 2005, 19:11
by AF
Pass a pointer to the struct instead, problem solved. Also sending in specific values might not be the best thing. As long as you send in the global structure you neednt worry about how it works underneath with regards to the rest of the prog as long as it does what ti says it does.

Lok at how JCAI and NTAI do it.

Posted: 14 Dec 2005, 19:17
by krogothe
right so it would work like this?

Code: Select all

void Xmas::SendPresent(*santabag my_bag) 
{ 
x = my_bag.toy1; 
y += my_bag.toy1 + sleigh; 
} 
santabag  gimmeprezzies; 
SendPresent(&gimmeprezzies);

Posted: 14 Dec 2005, 19:26
by AF
no, you're accessing a pointr so you use -> and not . operator.

Code: Select all

void Xmas::SendPresent(*santabag my_bag){ 
    x = my_bag->toy1; 
    y += my_bag->toy1 + sleigh; 
} 
santabag*  gimmeprezzies = new santabag; 
Xmas* x = new Xmas;
x->SendPresent(gimmeprezzies);

Posted: 14 Dec 2005, 19:33
by krogothe
I just thought it would work that way (using -> but passing the address of gimmeprezzies the same way)...

Thanks a lot either way!

Posted: 14 Dec 2005, 20:13
by SoftNum
Actually, technically both ways would work. But A's way is a bit more C++ friendly. Atleast, for the calling of the actual function. You would neet to use -> ... or the ponderous (*var).x -> is much better.

Posted: 14 Dec 2005, 22:08
by mr sharpoblunto
or another way of passing a pointer to the struct using reference variables

Code: Select all

void Xmas::SendPresent(santabag &my_bag){
    x = my_bag.toy1;
    y += my_bag.toy1 + sleigh;
}

santabag  gimmeprezzies;
SendPresent(gimmeprezzies);
 
this way you don't have to worry about using the -> operator and you don't have to worry about passing the address of the struct to the function. You can pass the struct itself, and the compiler will do all the pointer referencing and dereferencing transparently.

It does exactly the same as the above examples, it just depends on the semantics you prefer. :wink:

Posted: 14 Dec 2005, 22:10
by krogothe
but would that pass all the data in the struct, therefore making it slower?

Posted: 14 Dec 2005, 22:35
by AF
hmm I couldnt use the this keyword that way

Posted: 14 Dec 2005, 22:49
by Tobi
but would that pass all the data in the struct, therefore making it slower?
No.

it's the same as a pointer, but you use . instead of ->, and you can't make it point to something else..

Posted: 14 Dec 2005, 23:13
by mr sharpoblunto
hmm I couldnt use the this keyword that way
the method you use to pass parameters to a function shouldn't affect your ability to use the this keyword

Code: Select all

class test
{
  public:
    void testMethod(SomeOtherClass &x);
    void testMethod1(SomeOtherClass *x);
  private:
   int someAttribute;
};

void test::testMethod(SomeOtherClass &x)
{
   this->someAttribute = x.anotherAttribute;
}

void test::testMethod1(SomeOtherClass *x)
{
   this->someAttribute = x->anotherAttribute;
}
both approaches work. and yeah, like Tobi said using an & passes a pointer to the function, but you can use the . operator instead of the -> operator inside the function, and you don't need to use the address of (&) operator when passing the struct to the function.

Posted: 15 Dec 2005, 00:20
by renrutal
Be sure to use containers defined in the Standard Template Library(STL) and Boost library instead of hand-made ones, whenever it is possible:
  • They are very optimized.
  • Portable to all the important platforms.
  • Their manuals have time and space complexity descriptions of the actions you can do on them. It means you can choose which container would be less costly to the computer.
  • Support all kinds of data.
  • Build-in functions and operators you can use for all your actions.
  • Secure if used correctly(always use their operators!), meaning no memory leaks.
  • Containers are easy to access.
  • Iterators! No more problems looking for one data amont thousands in one container.
Really, they aren't a standard for nothing.

Posted: 15 Dec 2005, 18:24
by AF
What i meant was that in NTAI int eh globals class I initialize all the agents witht eh this keyword using thgisn such as L = new logger(this);

Posted: 15 Dec 2005, 22:25
by mr sharpoblunto
ahh right. because to get it to work you don't pass the parameter by reference you pass by value, then a pointer to that value is passed to the function for you.

Code: Select all

L = new logger(*this);
this should work.