con/destructor defaults

This is a discussion on con/destructor defaults within the C++ Programming forums, part of the General Programming Boards category; are any of these code blocks equivalent? Code: class c { int x,y; }; Code: class c { int x,y; ...

  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,046
    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
    21,446
    Are constructors/destructors supposed to have return types?
    I believe the answer is no.
    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

  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
    21,446
    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?
    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

  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
    21,446
    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.
    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

Popular pages Recent additions subscribe to a feed

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