explicit keyword

This is a discussion on explicit keyword within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I am reading the C++ Programming Language book, but can not find the function of explicit keyword of ...

  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
    21,712
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,317
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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, 07:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21