Two-phase search: can I avoid the "bloat code"?

Two-phase question: Is there a more synthetic way to write this code, i.e. Avoid all of these directives using? Something like using CBase<T>;this 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;
}

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

Thank you in!


gcc 4.1 macOS x 10.6

+3
source share
2 answers

I scaled down the test file 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
+2

, . CBase a? , CDer.

, POD, using CBase<T>::m_ints;.

: virtual.

, : CDer CBase, typedef .

0

Source: https://habr.com/ru/post/1739636/


All Articles