Actually my question is all in the title.
Anyway:
I have a class and I use explicit constructor:
.h
class MyClass
{
public:
explicit MyClass(const string& s): query(s) {}
private:
string query;
}
Is it obligatory or not to put explicit keyword in implementation(.cpp) file?
In both cases the constructor is inline. The only correct way to make it a regular out-of-line function would be to define it in the implementation file, not in the header.
To minimize the potential for errors, C++ has adopted the convention of using header files to contain declarations. You make the declarations in a header file, then use the #include directive in every .cpp file or other header file that requires that declaration.
To have all the information available, current compilers tend to require that a template must be fully defined whenever it is used. That includes all of its member functions and all template functions called from those. Consequently, template writers tend to place template definition in header files.
Why Do You Use Header Files? Header files are used in C++ so that you don't have to write the code for every single thing. It helps to reduce the complexity and number of lines of code. It also gives you the benefit of reusing the functions that are declared in header files to different .
No, it is not. The explicit
keyword is only permitted in the header. My gcc says:
test.cpp:6: error: only declarations of constructors can be 'explicit'
for the following code:
class foo {
public:
explicit foo(int);
};
explicit foo::foo(int) {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With