initialize strings in class

This is a discussion on initialize strings in class within the C++ Programming forums, part of the General Programming Boards category; Hello Suppose I have some class: Code: class someclass { public: someclass() : m_id(0) { } private: int m_id; std::string ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    initialize strings in class

    Hello

    Suppose I have some class:

    Code:
    class someclass {
    public:
      someclass() : m_id(0) { }
    
    private:
      int m_id;
      std::string m_str1;
      std::string m_str2;
    };
    Is it sensible to initialize strings in this constructor (when no arguments are passed) like : m_id(0), m_str1(""), m_str2("") , etc.. or just leave as it is?

    Thanks for help

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A string is by it's own constructor initialized to empty string, so you don't NEED to do it. It's of course nothing wrong with initializing it anyways [except it adds extra code].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thanks!

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It might be better to initialize them using the default constructor: m_id(0), m_str1(), m_str2()
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Somebody on this forum indicated that they prefer to always initialize everything in the initializer list, even those that only require a call to the default constructor. This documents that such an initialization was intended and not an oversight.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Daved View Post
    Somebody on this forum indicated that they prefer to always initialize everything in the initializer list, even those that only require a call to the default constructor. This documents that such an initialization was intended and not an oversight.
    Yes, there's truth to that - you KNOW that the value is initialized. And my comment that "it adds extra code" is probably a little bit exaggerated, as the default constructor will be called ANYWAYS for all members that are classes. So you're not really adding any extra calls.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    You're just adding a lot of extra work in typing that call to the default constructor that usually gets called by default
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    You're just adding a lot of extra work in typing that call to the default constructor that usually gets called by default
    Yes, but also clarifying to any casual reader that the content is initialized (and to what).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    It's another matter if you need to initialize it to something else than "empty," but otherwise there's no big gain in doing so.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    It's another matter if you need to initialize it to something else than "empty," but otherwise there's no big gain in doing so.
    Yes, but even in the "empty"/"default" value case, you clarify that you INTEND this to be empty/have default value, rather than "forgot to set it to something".

    Again, I'm not saying that you HAVE to do it either direction - but the REASON for having explicitly assigned the members in a class is that you indicate that "I know that this should be empty" rather than "it's empty because it hasn't been set yet" - those are two different cases. Have you ever had a function/class that didn't work because you forgot to set a member variable at some point or another? I find that highly likely.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    Probably there is, but I can't think of such a time, of course.
    Unless I have to initialize something to a value other than "empty," I never call the constructor explicitly. Every class should have a default constructor that initializes the class to "empty" IMO.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Unless there is no "empty" or "zero" state, in which case it shouldn't have a default constructor.
    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

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > Yes, but even in the "empty"/"default" value case, you clarify that you INTEND this to be empty/have default
    > value, rather than "forgot to set it to something".

    I understand the desire to convince the reader that the writer knows what he's doing, but this sort of thing can backfire. If the reader knows the language at least as well as the writer does, and notices that the writer did something explicitly, even though the default does the right thing, then he may start to wonder whether the writer even knew this, and start second-guessing everything else in the code. My philosophy is that if doing nothing is guaranteed to do the right thing, then it's safer to do nothing, and it's the reader's responsibility to know the language well enough to know the difference between whether the writer forgot something, or was just being concise.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, it's only meaningful to do implicit initialization explicitly if EVERYONE working on that code understands that it's done that way - e.g. in coding standards or such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    Quote Originally Posted by robatino View Post
    > Yes, but even in the "empty"/"default" value case, you clarify that you INTEND this to be empty/have default
    > value, rather than "forgot to set it to something".

    I understand the desire to convince the reader that the writer knows what he's doing, but this sort of thing can backfire. If the reader knows the language at least as well as the writer does, and notices that the writer did something explicitly, even though the default does the right thing, then he may start to wonder whether the writer even knew this, and start second-guessing everything else in the code. My philosophy is that if doing nothing is guaranteed to do the right thing, then it's safer to do nothing, and it's the reader's responsibility to know the language well enough to know the difference between whether the writer forgot something, or was just being concise.
    I agree with this. Keep It Simple.
    It's not worth trying to cater for every person that might not know one language feature or another.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on class coupling
    By andrea72 in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2011, 10:16 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. how to initialize a static pointer array in a class?
    By nadamson6 in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2005, 10:47 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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