Re: How to pass function name as a parameter?

From: Russ Holsclaw (russ_at_holsclaw.nyet)
Date: 05/25/04


Date: Tue, 25 May 2004 16:00:05 -0600


> There is also one question: what is the best method to convert overloaded
> index operators with return type of reference?
> for example:
> class A
> {
> private: int data[100];
> public: int& operator[] (int index) {return data[index];}
> }
>
> Now I can create function like this:
>
> Private data(100) As Integer
>
> Public Sub operator_index(ByVal index As Integer, ByVal NewValue As
Integer)
> data(index) = NewValue
> End Sub
>

I'm not sure what you mean by return type of reference, but if what you
want to do is have an object that acts like an array, you shouldn't
overlook what you can do with Properties, which don't really have a C++
counterpart.

----------in class clsArraything
Private data() As Integer

Private sub Class_Initialize()
   redim data(0) as integer ' create a trivial array
end sub

 Public Property Let value(ByVal index As Integer, ByVal NewValue As
Integer)
    if index > ubound(data) then
       redim preserve data(index)
    end if
     data(index) = NewValue
End property

Public Property Get value(byval index) as Integer
    if index > ubound(data) then
      value = 0 'pretend the array goes on forever, with zeroes
    else

      value = data(index)
    end if
end property
------------- end of class module

Now, to use this class, you declare an instance of the object:

Dim Myarray as New clsArraything

' ... and then you can set something into the array thus:

Myarray.value(I) = 295

'... and then address it like this:

X = Myarray.value(I)

... so you can make an object, or rather a property of it, behave just like
an array variable. C++ doesn't really have anything like VB's properties.
Notice that what we have here is an "array-like" object that behaves as if
it didn't have an upper boundary. It expands according to the data that's
put into it. (dealing with negative indexes is an exercise for the
student!)



Relevant Pages

  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... My questions was why the 2nd part of the sub did not change the changes the ... > changes to the array pass it byref. ... > ' new reference ... > ' allocate secondArray and copy its reference ...
    (microsoft.public.dotnet.languages.vb)
  • Re: newbie baffled by de/referencing with subroutines
    ... > and I want to use data from that array after the sub is finished. ... > then tried to print the data outside the sub by this dereference: ... > What's wrong with my reference and/or dereference, ...
    (comp.lang.perl.misc)
  • AW: returning hashes, and arrays
    ... It is much better to use reference when passing or retrieving more than ... Retrieve the values from a sub as refereces. ... I'm having trouble returning a hash from a subroutine, ... Must the hash and array really be a reference ...
    (perl.beginners)
  • Re: Perl Bug or stupidity on my part? - I looked in FAQs and dejanews prior to posting
    ... >value is a reference to an array to another sub. ... The only way I can picture that working is if $_ happened to have the ...
    (comp.lang.perl.misc)
  • RXParse module v.91 (by robic0)
    ... # Unicode character reference ... sub original_content ... then call content handler with $content ...
    (comp.lang.perl.misc)