Skip to content

Latest commit

 

History

History
71 lines (51 loc) · 1.93 KB

File metadata and controls

71 lines (51 loc) · 1.93 KB

using alias


Type aliasing

typedef std::ios_base::fmtflags Flags;
using Flags = std::ios_base::fmtflags;  // the same as above
Flags fl = std::ios_base::dec;
typedef std::vector<std::shared_ptr<Socket>> SocketContainer;
std::vector<std::shared_ptr<Socket>> typedef SocketContainer; // correct ;)
using SocketContainer = std::vector<std::shared_ptr<Socket>>;

Rationale: More intuitive alias creation.

A type alias is a name that refers to a previously defined type. It could be created with typedef. From C++11 type aliases should be created with using keyword.


Template aliases

template <typename T>
using StrKeyMap = std::map<std::string, T>;

StrKeyMap<int> my_map; // std::map<std::string, int>

Type alias can be parametrized with templates. It was impossible with typedef.

Template aliases cannot be specialized.


Constructors inheritance

struct A {
    explicit A(int);
    int a;
};

struct B : A {
    using A::A;  // implicit declaration of B::B(int)
    B(int, int); // overloaded inherited Base ctor
};
  • Derived class constructors are generated implicitly, only if they are used
  • Derived class constructors take the same arguments as base class constructors
  • Derived class constructor calls according base class constructor
  • Constructor inheritance in a class that adds a new field might be risky - new fields can be uninitialized

Exercise

Change a typedef to using alias.