Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive class definitions?

Status
Not open for further replies.

VBDotNetProgrammer

Programmer
Jul 9, 2003
50
0
0
GB
Hi All,

Im trying to write a class which contains itself as a private member, kind of like a recursive class definitions.

i.e.

class COperator
{
private:
COperator _operator;
public:
COperator();
~COperator();
};

Ive tried putting a prototype above it and it doesnt like that. It just spits out

error C2460: '_operator' : uses 'COperator', which is being defined

d:\fuzzyinferenceengine\rulebase.h(32) : see declaration of 'COperator'

Any help would be greatly appreciated.

Thanks
 
> COperator _operator;
You can only have pointers to COperator, otherwise you'd just fill up memory with nested copies of COperator.

Code:
COperator *_operator;

--
 
Right, think about what you're doing. If that would compile, when you created a COperator object, that object would need a member object, and then that member would need another member object, and then that would need one, and each objects takes some amount of memory, which means that you'd need an infinite amount of memory for this to work. Computer's don't have an infinite amount of memory :D.
 
If, instead of making it a private memeber, you just use it in a static member function, that would be OK I think. I'm not exactly sure what you are intending on using it for though. Use static member functions with careful thought.

Code:
 class COperator
   {
    private:
        COperator _operator;
    public:
        COperator();
        ~COperator();
        static void doSomething();
    };

void COperator::doSomething()
{
  COperator o = Operator();
}

-Bones
 
>you just use it in a static member function

Wouldn't be the equivalent of just using it in any function?

Code:
void somOrdinaryFunction()
{
  COperator o = Operator();
}

How do you mean that relates to COperator having a COperator as a member?


/Per
[sub]
www.perfnurt.se[/sub]
 
I would use the word "essentially" instead of "equivelent". For example...

Code:
 class COperator
   {
    private:
        COperator _operator;
    public:
        COperator();
        ~COperator();
    private:
        static void doSomething();
    };

void COperator::doSomething()
{
  COperator o = Operator();
}

He did say he wanted it private.

-Bones
 
Bones: Did you try compiling that? I'm pretty sure the compiler will complain quite loudly...

The COperator member variable must be a pointer for it to work.

VBDotNetProgrammer: What exactly are you trying to do? If we knew what the class was for it would help with creating a better solution.
 
Hi guys thanks for the replies.

Im busy writing a fuzzy logic based inference engine and needed a way to represent some rules which were recursive in nature as objects within the system.

When i say recursive I mean these rules obviously consist of antecedents and consequents. Both of which can contain multiple levels of operators i.e. logical AND / OR's etc connecting them.

I realised that the recursive class definition would disappear up its own a** during instantiation but was just hoping that there was some way of doing it :) since recursive functions are possible.

Anyway thanks for the time guys but ive come up with another method of representing the operators in levels which would mean I simply need to evaluate the rule in increasing order of precedense inorder for the rule integrity to be preserved.

Rgrds
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top