Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two-phase lookup: can I avoid "code bloat"?

Two-phase lookup question: Is there a more synthetic way to write this code, i.e. avoiding all those using directives? Something like using CBase<T>; is what I would like, but it is not accepted.

#include <iostream>

template <typename T>
class CBase
{
protected:
    int a, b, c, d;   // many more...

public:
    CBase() {
        a = 123; c = 0;
    }
};


template <typename T>
class CDer : public CBase<T>
{
//  using CBase<T>;     // error, but this is what I would like
    using CBase<T>::a;
    using CBase<T>::b;
    //...

public:
    CDer() {
        std::cout << a << this->c;
    }
};


int main()
{
    CDer<int> cd;
}

In my real code there are many more member variables/functions, and I was wondering if it is possible to write shorter code in some way.
Of course, using the this->c syntax does not solve the problem...

Thank's!


gcc 4.1 MacOS X 10.6

like image 974
Pietro Avatar asked Dec 07 '25 04:12

Pietro


1 Answers

I reduced the testcase and then consider three options

template<typename T> struct Base { int a; };

Option 1

template<typename T> struct Der : Base<T> {
  void f() { 
    int &ra = Der::a;
    // now use ra
  }
}

Option 2

template<typename T> struct Der : Base<T> {
  void f() { 
    // use this->a instead
    // or Der::a
  }
}

Option 3

// use your using declarations
like image 89
Johannes Schaub - litb Avatar answered Dec 08 '25 17:12

Johannes Schaub - litb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!