Thread: base class should be explicitly initialized in the copy constructor

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    anon: To generate the warning, I think this code will suffice. It also demonstrates the reason why the warning is there.
    Interestingly, the MinGW port of g++ 3.4.5 does not emit any warnings at -Wall, the Comeau online compiler does the same, and MSVC8 only warns about an unreferenced formal parameter at /W4. rockstar1707 used the MinGW port of g++ 3.4.5, so this seems inconsistent.

    And yes, in retrospect I interpreted rockstar1707's reported warning message completely wrong
    Last edited by laserlight; 11-20-2007 at 10:33 PM.
    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

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's what I meant. My example was a case where the warning was useful - because the resulting behaviour was most likely not what was intended.

    But from reading the Boost code that didn't have the problem, I knew that the base was default-initialized anyway, so the implicit behaviour was what was wanted. It's just that, on copy-construction, you typically want to copy-initialize the base, so if you actually do want to default-initialize it (which is what happens when you omit the base in the initializer list), the compiler asks you to do it explicitly - just to make sure you haven't simply forgotten the base.



    laserlight: Have you tried -Wextra? Or perhaps even -Weffc++?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    laserlight: Have you tried -Wextra? Or perhaps even -Weffc++?
    Ah, -Wextra did the trick, though -Weffc++ did not.
    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

  4. #19
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    Thanks again for all the answers. I must say that some things are unfortunately beyond my knowledge of C++...

    As I said, for now I'll ignore the warning, since I'm a bit in a hurry with development. But one day I'll try to take care of that as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. copy constructor prolbem(??) in a class Name
    By kantze in forum C++ Programming
    Replies: 15
    Last Post: 07-22-2007, 11:07 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM