Suggestion: Static function access to protected member of instance of its struct

Started by monkey0506, Thu 20/04/2006 17:58:23

Previous topic - Next topic

monkey0506

Would it be possible for static functions to have access to writeprotected members of instances of the same struct?

i.e., say I have a struct like this:
Code: ags
struct MyStruct {
  writeprotected int wpInt;
  import bool SetInt(int value);
  import static void RandomizeInt();
  };

(not a particularly good example...)

SetInt can be used to assign a new value to wpInt, but RandomizeInt has no access to it.  This is seemingly pretty similar to the question I asked before about struct member access specifiers.

I understand that it has been looked into, and people have gotten used to the way things are and I can't expect AGS to simulate C++ exactly...it's just a feature I would find very useful (and furthermore, there's no tracker entry for it).

In any case, I'd really like to see this implemented in this version of AGS.  And it seemed safer to ask here than create another thread very similar to the one I started before or dragging it back up after 8 months.

strazer

In your example, RandomizeInt is a static function so would be called from the struct itself, not an instance, like this:
  MyStruct.RandomizeInt();

Now, SetInt is a non-static member function and so, since there are no static variables in AGS, wpInt and SetInt can only be accessed by an instance of the struct:
  MyStruct TheThing;
  TheThing.SetInt(4);

How do you think would the RandomizeInt function know the variable of which instance to access?

Do I misunderstand you?

monkey0506

Because RandomizeInt (in the crappy example) is a static function, it would have to call upon an existing instance.  Now this is a particularlly bad example to display how this would work, but basically, it would go something like this:

Code: ags
MyStruct TheThing;

static void MyStruct::RandomizeInt() {
  TheThing.wpInt = Random(42);
  }


Like I said, the example is really bad for showing how this would work/be useful, but there is, I believe, a better example in the thread I linked to above.

One example of how it could be useful is static initialization of variables, that way each instance of the struct doesn't need to include initialization functions.  Now that's not a particularlly great example either...but...there is a very good reason for this...somewhere...(sorry I'm rather distracted right now with other things).

SSH

The question is begged, though: why do you need your randomize function to be static?
12

monkey0506

I don't.  It was just a really bad example.  I don't currently have a good one.  But there are some.  I mean, there's definitely reasons why other languages allow it.  I just can't think of one right now.

Scorpiorus

For example, I use a framework similar to this:

struct Struct {

   writeprotected bool created;
   import static int Create();

   int data;
};

Struct heap[100];

static int Struct::Create() {

   int i = 0;
   while (i<100)
   {
      if (heap.created == false)
      {
         heap.created = true;
         return i;
      }
      
      i++;
   }
   
   return -1;
}

...

int handle = Struct.Create();

heap[handle].data = 10;

I would be in favour of having writeprotected "created" but it's not possible at the moment.

SSH

This is why I said that, if using a module, then you can create an inherited struct/class that has your hidden variables in, inside the script rather than the header...
12

Scorpiorus

Unfortunately, hiding members in the internal struct is not very practical in this case.

"heap" is meant to be an exposed array and it's about creating instances here, not just having an interface struct of static members.

And "created" bool is supposed to be accessible for reading, and thus it must be a part of an instance.

__________
Anyway, allowing static member functions to access their struct members would be handy for initialization of arrays, etc...

Considering the time and efforts to implement it of course.

strazer

Scorpiorus, I added that link to his post. Isn't that what you referred to, SSH?

Scorpiorus

Ah, I see... But yeah, looking at the post itself, that seems to be it.

SSH

All of these things rely on knowing that there is going to be a particular instance of a struct existing, which may be appropriate for a module but isn't really for a static function of the class.

As for your heap problem, I think this would all go away if we could have structs of structs... in the simplest case, the answer is to have:


struct Struct {

Ã,  Ã, writeprotected bool created[100];
Ã,  Ã, import int Create();

Ã,  Ã, int data[100];
};


and just move those s around a bit... whioch would get messy with more complex arragements (see some of my modules, for example!), so better would be:

struct Structd {
  int data;
};

struct Struct {
   import int Create();
   writeprotected bool created[100];
   Structd data[100];
}

Or better still, dynamic arrays of structs ;)
12

Scorpiorus

Quote from: SSH on Fri 21/04/2006 15:51:36All of these things rely on knowing that there is going to be a particular instance of a struct existing, which may be appropriate for a module but isn't really for a static function of the class.

It's just that "heap[]" is statically defined because there is no way of allocating new instance at run-time.

Eventually, the example simulates the following behaviour:

Code: ags
static Struct* Struct::Create (int value) {

Ã,  Ã, Struct *instance = new Struct;
Ã,  Ã, instance.Data = value;
Ã,  Ã, return instance;

}


And it wouldn't compile if "instance.Data" is protected(writeprotected).

As an example, a C++ new operator that's defined within a class is a static function.

Quote
struct Struct {

Ã,  Ã, writeprotected bool created[100];
Ã,  Ã, import int Create();

Ã,  Ã, int data[100];
};

and just move those s around a bit... whioch would get messy with more complex arragements (see some of my modules, for example!)

Oh yes, I know what the mess you mean, experienced it too... That's why I follow that approach instead.

QuoteAs for your heap problem, I think this would all go away if we could have structs of structs...

Hehe, sure but I need to work with it somehow at the moment...

QuoteOr better still, dynamic arrays of structs ;)

At least supporting static arrays of pointers to user-defined structs would be a wondeful addition.

The "new" keyword would make deal even better! Having it was mentioned to be possible at some point, if I'm not mistaken.

So... "new" to spawn them at run-time and "arrays of pointers to user-defined structs" to make nested structures. The codes would look much more readable I have to say.


:)

monkey0506

So CJ, how about an increase in AGS struct functionality somewhere? There are several places where increased functionality could be extremely useful, so even just changing one of them would be great.

Edit by strazer: Worded nicely.

[EDIT:]

Lol...I didn't mean it to be rude it the first place.  In fact I was laughing when I typed it.

Pumaman

Rather than doing something like this I'd rather just add support for static variables, but of course I can't promise when that might be.
.

SMF spam blocked by CleanTalk