Thread: con/destructor defaults

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

    con/destructor defaults

    are any of these code blocks equivalent?
    Code:
    class c
    {
        int x,y;
    };
    Code:
    class c
    {
        int x,y;
        c() {};
    };
    Code:
    class c
    {
        int x,y;
        c();
    };
    
    c::c(){}
    edit: I almost forgot, the same question for destructors too.
    Last edited by robwhit; 06-30-2007 at 12:29 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No. Well, sort of.

    The first one uses the built-in default constructor. I'm not sure how it's implemented, but it's probably the same as the second version.

    The second one declares and implements the constructor inline. (Because the function body is inside the class.) When a function is inline, this suggests to the compiler (it can ignore the request) that the function is so simple that where ever it is used, the code in the function is put there instead of a function call to this trivial function. It's a good idea to make set and get functions as well as empty ones inline like that.

    The last one makes the constructor like a normal function. But the compiler is free to optimise and make any functions inline that it thinks should be inline, and in fact most compilers ignore the inline suggestions by the programmer and make their own decisions. They're much better at it than we are.

    So they're probably the same, in the end, with optimisations and all.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    The the second one
    Code:
    c() {};
    this ; is not required.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    oh ok

    Quote Originally Posted by noobcpp View Post
    this ; is not required.
    oh, right. *smacks head*

    I've just thought of another question: Are constructors/destructors supposed to have return types?

    thanks
    Last edited by robwhit; 06-30-2007 at 01:01 PM.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Simple Answer Nope
    cause They Dont need.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are constructors/destructors supposed to have return types?
    I believe the answer is no.
    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

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    so it's impossible to have a function pointer to a constructor or a destructor?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    so it's impossible to have a function pointer to a constructor or a destructor?
    I would reason yes, since constructors do not have names. Without a name, how would one point to a function?
    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

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    ok I tried this
    Code:
    class c
    {
    public:
        int x,y;
        c();
    };
    
    c::c(){}
    
    int main()
    {
        typeof(c::c)*var;
    
        var=c::c;
    
    
        return 0;
    }
    Code:
    main.cc: In function `int main()':
    main.cc:14: error: expected primary-expression before ';' token
    what does that mean?
    constructors do not have names
    Isn't it the same name as the class?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Isn't it the same name as the class?
    From what I understand, no. The syntax just happens to involve the class name, but it is really different from normal function declaration, even if it looks similiar.
    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

Popular pages Recent additions subscribe to a feed