+ 2
what do the words "noexcept, explicit, override" mean?
#define LOVE noexcept #define DEATH explicit #define BEAUTY override
3 ответов
+ 7
"noexcept" keyword denotes that a function won't propagate exceptions. If an exception is thrown, the exception won´t propagate to caller functions and program aborts in that function. Example:
void fun2() { throw 1; }
void fun1() noexcept {
// if fun2 throws an exception
// program aborts here
fun2();
}
"override" keyword in C++ is used in a similar way that @Override in java. It means that the funcion is overriding a virtual funcion. If there isn't a virtual funcion with that name it will generate an error. This is useful for avoiding typos. Example:
class A {
virtual void foo()=0;
};
class B : A {
// tried to override virtual foo function in base
class A. Typo fooo instead of foo will generate an error due to override keyword
void fooo() override {}
};
"explicit" keyword avoid object implicit initialization of classes with one parameter in order to avoid typos. Example:
class A {
public:
int data;
A(int i) { data = i; }
};
// Not allowed
A a ={ 3 };
// Must be
A a(3);
+ 8
Ops ! sorry. This is the corrected example:
Explicit avoid object implicit initialization of classes with one parameter in order to avoid typos. Example:
class A {
public:
int data;
explicit A(int i) { data = i; }
};
// Not allowed
A a ={ 3 };
// Must be
A a(3);
+ 2
thanks guy. your explain is very good but you didn't use from explicit keyword in your example.