Explicit keyword

This is a discussion on Explicit keyword within the C++ Programming forums, part of the General Programming Boards category; What can I do? Something had to not get inside my thick skull. The explicit keyword is the one. Just ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412

    Explicit keyword

    What can I do? Something had to not get inside my thick skull. The explicit keyword is the one. Just can't get it.

    Why is that the following code is working. Why does it allow the implicit conversion?

    This is the class:
    Code:
    class Range {
    public:
        explicit Range(const int i): sInt_(i) {
            if(i < 0 || i > 255)
                throw std::out_of_range("Invalid small integer. Out of range.");
        }
        
        operator int() const { return sInt_; }
    private:
        size_t sInt_;
    };
    and on main...
    Code:
    double i = 12;
    Range si(i);
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It's allowing a conversion from double to int. That's different than a conversion from int to Range.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Yes. I was not clear and forgot to remove the operator int().

    I mean exclusively the explicit defined construct. Shouldn't the explicit keyword on this case negate the implicit conversion from double to int?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That's what I was referring to. The explicit keyword on a constructor only governs conversions to the class to which it belongs.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If you want code:
    Code:
    void Foo(Range r)
    {
    //...
    }
    //...
    double d = 5;
    Foo(d); //error, asking for an implicit conversion
    Foo(Range(d)); //no error (implicit conversion from double to int accepted)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Ah! Got it! Thank goodness.

    Thanks both.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

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 George2 in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2007, 12:57 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