Raised This Month: $ Target: $400
 0% 

Inheritance


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
zeroibis
Veteran Member
Join Date: Jun 2007
Old 09-15-2013 , 15:35   Inheritance
Reply With Quote #1

I wanted to get a better understanding of using inheritance across methods.

For example can I make a set of stocks like:
Code:
stock methodA()
{
new X;
new Y;
methodB(X, Y);
return X+Y;
}

stock methodB(X, Y)
{
X = 1;
Y = 1;
}
The above should output 0 for methodA. How do I make it so that methodB can inherit the variables from methodA and modify them so that the output of methodA will be 2? Or is this not possible in pawn?
__________________
zeroibis is offline
xf117
Senior Member
Join Date: Mar 2010
Location: Russia
Old 09-15-2013 , 15:53   Re: Inheritance
Reply With Quote #2

There is no Inheritance here. Cells are passed by value, so in your case they won't be modified.
Use stock methodB(&X, &Y)
This should work, i think...
xf117 is offline
Send a message via ICQ to xf117
zeroibis
Veteran Member
Join Date: Jun 2007
Old 09-15-2013 , 16:09   Re: Inheritance
Reply With Quote #3

Oh wait so if I use &X it will then inherit!?
__________________
zeroibis is offline
zeroibis
Veteran Member
Join Date: Jun 2007
Old 09-15-2013 , 16:11   Re: Inheritance
Reply With Quote #4

What confuses me is that I have seen method like this:

Code:
/**
 * Fills an array with a given value in a 1 dimensional static array.
 * You can specify the amount of cells to be written.
 *
 * @param array            Static Array.
 * @param size            Number of cells to write (eg. the array's size)
 * @param value            Fill value.
 * @param start            Optional: Offset where to start (0 - (size-1)).
 * @noreturn
 */
stock Array_Fill(any:array[], size, any:value, start=0)
{
    if (start < 0) {
        start = 0;
    }

    for (new i=start; i < size; i++) {
        array[i] = value;
    }
}
Is it just able to work with an array b/c of the variable type it is?

Using any, any:y does not work as expected.
__________________
zeroibis is offline
xf117
Senior Member
Join Date: Mar 2010
Location: Russia
Old 09-15-2013 , 16:17   Re: Inheritance
Reply With Quote #5

Arrays and Strings are passed by reference -> they are mutable. Any: basically means it can be almost any type, cell or float in this case.
It's good idea not to use it, really. There are few uses when you really need it.

Last edited by xf117; 09-15-2013 at 16:20. Reason: clarification
xf117 is offline
Send a message via ICQ to xf117
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 09-15-2013 , 16:20   Re: Inheritance
Reply With Quote #6

any: should not be used in that way. To answer your question, arrays are automatically passed by reference. Also, passing by reference is not called "inheritance" - that is something unrelated to SourcePawn.
__________________
11530 is offline
necavi
Veteran Member
Join Date: Sep 2010
Old 09-15-2013 , 19:04   Re: Inheritance
Reply With Quote #7

In fact, if I remember correctly from poking around sourcecode, all &<variable> does is basically create an array of size 1, and then pass it through array passing mechanisms.
necavi is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 09-15-2013 , 19:29   Re: Inheritance
Reply With Quote #8

Quote:
Originally Posted by necavi View Post
In fact, if I remember correctly from poking around sourcecode, all &<variable> does is basically create an array of size 1, and then pass it through array passing mechanisms.
That would be a sane implementation.
__________________
asherkin is offline
zeroibis
Veteran Member
Join Date: Jun 2007
Old 09-16-2013 , 01:19   Re: Inheritance
Reply With Quote #9

Ah ok so that explains everything I was trying to wonder why it appeared that only arrays would work. Also thanks for the info on the & as I now understand what that does and why it works.

BTW was there a doc sheet anywhere that I missed that explained these things in detail or did this stuff just fly over my head due to lack of general programing knowledge?

Also thanks the & works perfect and does exactly what I needed. Hooray for less duplicate code!
__________________

Last edited by zeroibis; 09-16-2013 at 01:36.
zeroibis is offline
BAILOPAN
Join Date: Jan 2004
Old 09-16-2013 , 02:29   Re: Inheritance
Reply With Quote #10

The implementation of & works the same way it does in C, it passes the address that contains the variable (in the compiler internals its storage class is "iREFERENCE", rather than iARRAY or iVARIABLE).
__________________
egg
BAILOPAN is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:05.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode