views:

411

answers:

2

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?

+9  A: 

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) {}
Greg Hewgill
A: 

Re the followup question (which you really should have submitted as a separate question), the initialization list goes with the constructor's implementation (its function body), which might be in either the header or the cpp file.

Joe Ganley