Conversion operators can now be marked
as explicit.
Monday, July 31, 2017
C++11: Override Controls: final
You can declare a function to be final
in a base class, which will prevent any derived class from overriding that
function.
C++11: Override Controls: override
You can tell the compiler that you are attempting
to override a function, so that the compiler can check whether you are really
overriding the function or just overloading the function.
C++11: Inherited Constructors
You can pull Base Class Constructors into a Derived Class’ scope. Here is an example:
#include <iostream>
class Base
{
public:
Base(int i) {std::cout << "Base One Parameter CTOR called. ";}
};
class Derived : public Base
{
using Base::Base; // Without this line, the program would not compile.
};
int main()
{
Base base(1) ;
Derived derived(1);
return 0;
}
// Output: Base One Parameter CTOR called. Base One Parameter CTOR called.
Reference: https://isocpp.org/wiki/faq/cpp11-language-classes#delegating-ctor
C++11: In-class member initializers: non-static data members
Non-static data members can be initialized where they are declared in the class.
Here is an example:
https://isocpp.org/wiki/faq/cpp11-language-classes#member-init
#include <iostream>
class A
{
public:
int a = 1;
};
int main()
{
A a;
std::cout << a.a << std::endl;
return 0;
}
// Output: 1
References: http://en.cppreference.com/w/cpp/language/data_membershttps://isocpp.org/wiki/faq/cpp11-language-classes#member-init
C++11: Delegating Constructors
You can now call a constructor from the MIL (Member Initialization List) of another constructor from the same class.
#include <iostream>
class A
{
public:
int m_a;
A() : A(42) {}
A(int a) : m_a(a) {}
};
int main()
{
A a;
std::cout << a.m_a << std::endl;
return 0;
}
// Output: 42
Reference: https://isocpp.org/wiki/faq/cpp11-language-classes#delegating-ctor
C++11: =delete
Specifying =delete for any of the compiler-generated constructors or assignment operators, makes them unavailable.
Here is an example:
#include <iostream>
class A
{
public:
A() {std::cout << "Default CTOR called; ";}
A(const A& a) = delete;
};
int main()
{
A a1;
// A a2(a1); // This line does not compile because of " = delete' above.
return 0;
}
// Output: Default CTOR called
Reference: https://isocpp.org/wiki/faq/cpp11-language-classes#default-delete
Subscribe to:
Posts (Atom)
