简体   繁体   中英

Accessing member functions of other classes into member function of `this` class using composition or friend classes

I am writing a class using 'composition' as follows -

class fibonacci
{
private:
    FibonacciDynamic dy();
    FibonacciRecursive re();
    FibonacciSequential se();
    int count;
public:
    fibonacci(int a):count(a){};
    void disp();
};

void fibonacci::disp()
{
    if(count < 20)
    {
        se.fib();
    }
    else if(count < 50)
    {
        re.fib();
    }
    else
    {
        dy.display();
    }
}

Here, FibonacciDynamic , FibonacciRecursive & FibonacciSequential are classes declared in header files. Now, the main problem here is that while using se.fib() , re.fib & dy.fib() functions it gives me error like

error C2228: left of '.fib' must have class/struct/union

Is there any other way to use composition approach here without getting above errors?

If not then is it possible to use them as friend classes & access their member functions in a member function of fibonacci class?

Thanks.

FibonacciDynamic dy(); is declaration of method FibonacciDynamic fibonacci::dy();

you should remove parentheses to make it data member declaration:

...
FibonacciDynamic dy;
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM