Thread: explicit keyword

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    explicit keyword

    Hello everyone,


    I am reading the C++ Programming Language book, but can not find the function of explicit keyword of constructor. Could anyone explain its usage or refer some learning materials please?


    thanks in advance,
    George

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A simple example is:
    Code:
    class X
    {
    public:
        X(int x) : x_(x) {}
    private:
        int x_;
    };
    
    int main()
    {
        X a(1);
        X b = 2;
    }
    If we add the explicit qualifier to the the constructor for X, the "X b = 2;" statement will result in a compile time error. The idea is that using this syntax (constructor used for implicit conversions) can be misleading under some circumstances and so should not be allowed at those times.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    explicit means you must pass the proper parameter types to your explicit constructor (i.e. the types can't be automatically converted to the proper type for you).

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Perhaps a better example might be this:
    Code:
    class X
    {
    public:
        explicit X(int x) : x_(x) {}
    private:
        int x_;
    };
    
    void foo(X x);
    
    int main()
    {
      X x1(3);
      foo(x1); // ok
      foo(X(3)); // ok
      foo(3); // not ok because of explicit
    }

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If there is a class A and a class B:

    Code:
    class A
    {
    };
    
    class B
    {
        B(const A &obj);
    };
    And a function F:

    Code:
    void F(B obj)
    {
    }
    Then you are allowed to call F with an object of type A, since it can be converted to an object of type B by the B(const A &) constructor. If you do NOT want this to be allowed, you must qualify the B(const A &) constructor with the "explicit" keyword.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interesting behavior with explicit copy constructor
    By Clairvoyant1332 in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2008, 03:19 PM
  2. Explicit keyword
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2006, 06:43 PM
  3. Virtual Keyword
    By Shal in forum C++ Programming
    Replies: 6
    Last Post: 05-18-2006, 11:37 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. how to search a keyword in C?
    By kreyes in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 08:22 PM