473,563 Members | 2,767 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recursive delegation with non-static functions?

I'm playing around with a class design using delegates and have gotten
stuck. I'm not certain if cooler heads can prevail or if it is forces
that man was not intended to tamper with.

The LazyArray class below is for processor-intensive sequences, like
recursive functions. It doesn't calculate any values that you don't
ask for and remembers the ones that it has calculated in the past so as
to not do it again. (In practice, the integer array would be replaced
with a Hashtable, I suppose, but I wanted to keep things simple for the
moment.)

The program does not compile, alas, since the static functions are
referencing items that are not in their context. I can't figure out
how to bring everything under the same tent, though. The thing that's
getting under my skin is that this DOES work if I make F and M
unrelated classes with all-static members, but as soon as I start
gathering their common functionality under a base class I'm socked with
this problem or trying to make a static virtual function.

Is there a way to untie this knot, or am I stuck?

Thanks,
Matthew
--
class Program2
{
public class LazyArray
{
public delegate int Calculate(int n);
private Calculate do_it;
private int[] known = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1 };

public LazyArray(int val_at_zero, Calculate theFunction)
{
known[0] = val_at_zero;
do_it = theFunction;
}
public int this[int n]
{
get
{
if (known[n] == -1) { known[n] = do_it(n); }
return known[n];
}
}
}

public static int f_at_n(int n) { return M[F[n - 1]]; }
public static int m_at_n(int n) { return F[M[n - 1]]; }
static void Main(string[] args)
{
LazyArray F = new LazyArray(0, f_at_n);
LazyArray M = new LazyArray(1, m_at_n);
Console.WriteLi ne(F[10]);
}
}

Nov 16 '05 #1
1 1847
The problem appears to be that your delegates don't take enough
arguments. You give them an integer and nothing more. You need to pass
in everything they need in order to do their jobs, or they won't be
very good delegates.

However, you can't really pass in the array, because this can get you
into infinite recursive loops. You'll have to think fairly carefully
about exactly what the delegate functions are expected to do, and
whether they see the lazy array as a LazyArray (dangerous, since this
can result in calls back to themselves) or as a simple array of ints
(dangerous, as you then expose your internal structure to the
delegates).

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4420
by: DPfan | last post by:
Is the following so-called "delegation"? If not how to make some changes so that the F class delegates its operation to an E instance. On the other hand the following code runs without any problem. Is there any potential problems with it? class E { public: void Draw_E(int a, int b) { cout << "Draw in E " << a*b<< endl; }
6
11201
by: Ravi | last post by:
Hi, I need to find the non-recursive algorithm to find the height of a Binary Tree. Regards, SunLight.
1
1140
by: Jack | last post by:
Hi, I am writing a COM server with ATL and VC++.NET. I wonder if there is a way to reuse COM method implementations without delegation, something like this: __interface IFoo : IDispatch { HRESULT MyMethod(); // other method declarations... }
1
1145
by: jm | last post by:
I promise. I have tried everything I could find. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT05.asp (and much more) I have an asp (not .net) page on a server which is not a domain controller (2003 server in a 2000 forest). The asp page on the server hits a database on another server. For...
2
2427
by: russell.lane | last post by:
I'm building out a pretty standard n-tier web application. The stack includes application/presentation, biz logic, and data access layers on top of an SQL server back end. We want to use impersonation and delegation to forward the user's Windows login through all layers in the stack. To support this, I'm setting up a set of domain...
9
3317
by: seberino | last post by:
I'm a compiler newbie and curious if Python grammar is able to be parsed by a recursive descent parser or if it requires a more powerful algorithm. Chris
6
2829
by: Marc Castrechini | last post by:
This is a classic double hop delegation issue, however its the first time we are setting this up so we are doing something incorrectly. If we run through the IDE or using a localhost path on the web server the command succeeds. However, if we use the servername or ip through IIS it fails. For this reason we know we have permissions setup...
5
1156
by: Marc Castrechini | last post by:
We are currently using impersonation with constrained delegation to run a dual server environment for ASP.NET 2.0 and SQL Server 2005. Everything is up and running great, however, we have the following issue and are not sure where to look. If a user is logged into our Application Server and is inactive for a certain amount of time (to be...
1
2371
by: abc12334 | last post by:
Im supposed to create a recursive and non recursive function to calculate this. F(n) = 1 for n = 0 or n = 1 or n=2 F(n) = F((n+1)/2)^2 + F((n-1)/2)^ 2 for n is odd F(n) = F(n/2 + 1)^2 – F(n/2 – 1)^ 2 for n is even I know how to do the recursive one which looks like this and returns the fibonacci numbers if (n == 0 || n == 1...
13
1447
by: barcaroller | last post by:
What is the common way/design-pattern (if any) in C++ for delegating function calls that are not handled by a certain class. Public inheritance would be one way but not all classes are meant to inherit from (e.g. STL). Example: class A {
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.