Thread: automatic constructor defaulting

  1. #1
    Registered User
    Join Date
    Jan 2005

    automatic constructor defaulting


    I just came across this "feature" in c++:

    class Blah{
          Blah() {};
          Blah(const string& thestr) {};
          Blah(const char* mwah) {}
    void moo(const Blah& theblah){
       // do nothing
    int main(){
    So obviously moo only should be taking a Blah class, but somehow it can also take a char* and a string? What is this feature called? I'm just making sure this is standard c++..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    This is in the realm of standard C++. A temporary is created in each case, and the const reference parameter can bind to this temporary. If you wish to prevent this implicit conversion, declare the respective constructors as explicit, e.g.,
    explicit Blah(const string& thestr) {}
    explicit Blah(const char* mwah) {}
    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
    Registered User
    Join Date
    Dec 2006
    Hmm. Interesting. Learned something new here. So that's why functions expecting a std::string can be thrown a "const char *", too.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Yeah, I think this would be useful as a way to lazily implement overloading.. heh

    thanks laserlight

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why automatic variables are assigned with garbage values
    By srinivasg in forum C Programming
    Replies: 1
    Last Post: 11-08-2005, 07:14 AM
  2. automatic variables
    By FOOTOO in forum C Programming
    Replies: 5
    Last Post: 03-08-2005, 06:30 PM
  3. Automatic vs. Static Duration
    By JMB in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2002, 07:16 PM
  4. Automatic Time in C++Builder ??
    By Gugge in forum Windows Programming
    Replies: 0
    Last Post: 06-18-2002, 12:58 PM
  5. Automatic <cr> in User Inteface
    By chubaka in forum C Programming
    Replies: 3
    Last Post: 01-14-2002, 01:06 PM