Calling a base class (struct) member?

Started by Crimson Wizard, Tue 15/06/2010 06:21:48

Previous topic - Next topic

Crimson Wizard

I have a feeling that this was already discussed, but I couldn't find anything using search so far.

No, I am not suggesting anything, I am merely asking.
I know it is possible to override extender function of the base struct for child struct in AGS. But is it possible to call base struct's function variant from child's class overriding function?

BTW, AGS documentation does not mention anything on this topic (hint hint).

monkey0506

#1
You have to write a conversion method..which can be slow and/or complicated if you're working with dynamic instances. You can take a look at my Stack module for some thoughts on vectorization of dynamic data. I assume you're familiar with the article I wrote in the wiki on polymorphism.

That said if you're instead working with a static instance or static array of instances then it's simpler..so I'll show you how to do that before leaving you to your horrible, doomed fate. :)

Code: ags
struct Parent {
  import void SetData(int);
  import int GetData();
  protected int data;
};

struct Child extends Parent {
  protected int child_data;
};

void Parent::SetData(int value) {
  this.data = value;
}

Parent parents[5];

void SetData(this Child*, int value, int parentID) {
  if ((parentID < 0) || (parentID >= 5)) AbortGame("wtf man?");
  this.child_data = (value % 5);
  parents[parentID].SetData(value / 5);
}

int Parent::GetData() {
  return this.data;
}

int GetData(int parentID) {
  if ((parentID < 0) || (parentID >= 5)) AbortGame("seriously wtf?");
  return ((parents[parentID].GetData() * 5) + this.child_data);
}


I abstain myself of all responsibility if there's anything wrong with the code including (but not limited to :P) compilation errors, lack of real-world usability or usefulness, etc. for the sheer and simple fact that I am writing this from my roommate's phone.

The reason it's not referenced in the manual is probably due to the fact it's not technically a supported mechanism and is in fact a beautifully little beneficial fluke in the implementation of extender methods that I discovered while toying around to see what AGS would let me get away with, as I so often do. :)

If you can manage to vectorize your data (see the guide on extending the Stack module) then the aforementioned article in the wiki should contain sufficient information on accomplishing this goal with dynamic instances.

Good luck you poor, doomed child. :=

Monsieur OUXX

monkey, you just couldn't resist  :D
You just COULDN'T! And just after a dramatic post saying "hey, I don't have the Internet, don't expect me to post ANYHTING, like, seriously, ANYTHING".  ;)

I think that's cool. Ars gratia Artis!

I have a question, though.
Correct me if I'm wrong. In your code sample, in order to be able to call the original version of "SetData" (the one originally defined for "Parent"), you actually store copies of all the parents. However, don't the chidren contain their own value of the the member "data"? (if not, then I'm too rusty with AGS script).

Why doing all this duplication, instead of just keeping one and only instance of a "Parent" object that you'd call whenever you want to borrow its "SetData" ?

Oh, wait a minute, is it because you're afraid you can't predict if several calls to this method will be made simultaneously, and therefore you fear a collision of all those calls on your poor, single Parent instance? And to solve that you keep a pool of Parents where you can pick any available Parent?



 

Crimson Wizard

monkey, I'll take your answer as "you can but you better not do" :P

Quote from: Monsieur OUXX on Fri 18/06/2010 10:57:56
Correct me if I'm wrong. In your code sample, in order to be able to call the original version of "SetData" (the one originally defined for "Parent"), you actually store copies of all the parents. However, don't the chidren contain their own value of the the member "data"?
Yep, they do.

Quote from: Monsieur OUXX on Fri 18/06/2010 10:57:56
Why doing all this duplication, instead of just keeping one and only instance of a "Parent" object that you'd call whenever you want to borrow its "SetData" ?
The whole code above is a screwed workaround which does not make much sense and has an effeciency close to zero. :P

Monsieur OUXX

#4
Quote from: Crimson Wizard on Fri 18/06/2010 13:04:17
The whole code above is a screwed workaround which does not make much sense and has an effeciency close to zero. :P

Wow, you're harsh.
Assuming I understood it properly (the array of "Parent" objects is there only to temporarily instanciate Parents and then give them back to the pool), then it's pretty much the only way. Monkey is not to blame for that, it even allows him to do elegant things based on those ugly workarounds. The only thing that annoys me is all those copies and function calls, that, as you reminded it, are *so* slow.

That's exactly why I started writing AGSH in the first place: pass everything "by reference", and use only static functions, so that I can choose which one I want to call.

This has nothing to do with this thread, but I'm also starting to think I'll write a tiny pre-processor that will allow the use of the "inline" keyword (you know, that thing that doesn't make much sense anymore in C++).
 

Crimson Wizard

#5
Monsieur OUXX, I am not blaming monkey, I was just being sarcastic. Or having fun. :)

From what I understood, AGS needs a support for pointers to custom type objects. That should fix the problem in some way.

monkey0506

You people seriously need to look at the Stack module already. True, serialization of the data into a String and back is significantly slower than direct pointer or  reference access..however, the Stack module did prove that it is possible in AGS. :P

Even pointer types could be serialized with special exceptions being made to persist the data as needed. I was working on a specialized list type to that end but without script struct templates the code repetition is ridiculous.

Anyway..enough phone usage. Bye. :)

Crimson Wizard

#7
Well, I already did use the Stack module... and maybe it's a working workaround... but I don't feel like using it right now. Workaround, I mean, not Stack module.

SMF spam blocked by CleanTalk