Hello cplusplus.com Forums,
I am pretty new to programming and I have been searching for ways to write classes and templates with recursive function and data members:
1 2 3 4 5 6 7
|
// something like this:
class A {
int a;
int b;
A * myFunction (any_type); // this would be cool to do . . .
}
|
I'm wanting to wrap up my procedural style functions into OOP with templates so that it can be reused from a header inclusion and an object. This would be helpful in that every function that is dependent on a particular object would belong to the object and have the access restriction that is specific to the class body. If I could do this with a template, then I could try to re-implement a interesting sort routine that rearranges integers into a template that sorts arrays of objects and other types.
My issue is that I have searched around and I have not found a clear example of the technique anywhere, except for this one:
and this I am not understanding this example. I tried to use something similar within a test object that was supposed to demonstrate the effect, but I couldn't get it to work.
I also wanted to do something along the lines of nested classes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
class B {
// arbitrary stuff . . .
public:
struct C;
void aFunctionOfB (any_type);
} objectB;
class B::C {
// arbitrary stuff . . .
public:
C * aFunctionOfC (any_type);
}
C * B::C::aFunctionOfC (any_type) {
// arbitrary definition . . .
}
void B::aFunctionOfB (any_type) {
C objectC = *aFunctionOfC (any_type); // I'm not sure of the syntax . . .
/* so if aFunctionOfC() wasn't a class member . . .
C objectC = aFunctionOfC (any_type); // this works fine, but its a
non-pointer */
}
|
I got the basic ideal from these articles:
working with pointers is easy enough until I start dealing with pointers to objects or object members. I'm really not sure how everything works then. But I'm not able to get a class recursive function to work syntactically with or without a pointer, but those articles I mentioned have a few people saying that the recursive functions that use the local class as their return type (
that's what I'm talking about - member functions that use the local class as the return type within the scope of the local class, in case calling it class/template recursion is incorrect. . .) use a pointer for some reason. But in any event, if it allows the flexibility that it seems to provide in with the benefits of OOP, then it's worth learning and will be very useful to me.
Is their anyone who might be willing to direct me to a web link that explains these techniques more thoroughly, or might be able to give an example of both the ideal of using a local class as the return type from within the scope of the local class and/or how to properly nest classes? I hope that I am conveying the right ideal about the nature of my problem, as it might be an important technique to learn, or, as far as I know, there could be a better way to accomplish this. The
friend keyword could probably be used, but I've listened to quite a few people that recommend not using friend as it overrides the class access restrictions, so it could produce problem code that malfunctions in a way that's difficult for a novice programmer to root out.
I have spent quite a bit of time searching for "class recursion c++" and other variants of the question. Any help that could be provided will be appreciated, whether I'm smart enough to follow it or not, but I assume many of you are able teachers and qualified to give adequate help to those that come looking for. If I need to give more specific information about the problem, please indicate that and I will try to accommodate you. If I have indicated the wrong problem in my thread title, "Class & Template Recursion," please inform me of the correct concept that I am requesting. Perhaps that is a reason why I am unable to find the answer to this issue.