Thread: should i always write a constructor for a class?

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    26

    should i always write a constructor for a class?

    I know that visual studio do this for me as a default.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    If you do not need to do explicit initialization of your object, you do not need to define a constructor.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    A default constructor is already pre-defined regardless (if there are none defined), unless you have an overloaded one I believe. Otherwise, the object would never be able to be created/instantiated, because no constructor can be called.

  4. #4
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    You only need if you want to initializes an instance of its class which is how a class does anything.
    UPDATE! As of 10/6/2014

    https://www.dropbox.com/s/2sj6qwpfbb...t%201.zip?dl=0
    Just find the application file and double click it. Controls are (Arrow keys) (Z) (Z + arrow key) (Spacebar)
    Don't play this crappy update. Wait for the newest one which is far more impressive.

    Official Sonic character poll hosted by some guy at Sega..... Sega of America. Vote for blaze because she OP.
    http://blogs.sega.com/2015/08/28/the...down-heats-up/

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    In general, if the default constructor is sufficient, you don't need to define your own empty one. You may want to define one if your class contains primitive types that would need to be initialized (although you can just initialize the members inline in C++11), or if there's some other setup you class needs to do.

    Quote Originally Posted by cstryx View Post
    A default constructor is already pre-defined regardless (if there are none defined), unless you have an overloaded one I believe. Otherwise, the object would never be able to be created/instantiated, because no constructor can be called.
    You also don't get a default constructor if any of the class members can't be default constructed, such as if there are reference members.
    Last edited by King Mir; 08-28-2014 at 10:42 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,480
    A default constructor is already pre-defined regardless
    No a default constructor is only provided by the compiler if there are no other constructors provided. If you provide a constructor with any number of arguments the default constructor will not be provided, and you will need to provide the no-argument constructor if there is a need for this constructor.

    But if you don't create any constructors the compiler will produce a default no argument constructor.


    Jim

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I'd go so far as to call it a mistake to provide a constructor when it would be generated for you, and has no work to do besides what it does when you let it generate this on its own.

    I.e. More code for the sake of more code, is a bad thing.
    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"

  8. #8
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Quote Originally Posted by jimblumberg View Post
    No a default constructor is only provided by the compiler if there are no other constructors provided. If you provide a constructor with any number of arguments the default constructor will not be provided, and you will need to provide the no-argument constructor if there is a need for this constructor.

    But if you don't create any constructors the compiler will produce a default no argument constructor.


    Jim
    0.o... Ok, firstly thanks for quoting part of my post and ignoring the rest as though you're trying to say that I'm wrong, however this *is*, exactly as I said. Read my post again and see my remark stating "(if there are none defined)"...

    Did you only read half of my post? Please read the full thing next time, just some advice.
    Last edited by cstryx; 08-29-2014 at 07:56 PM.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,480
    Did you only read half of my post? Please read the full thing next time, just some advice.
    I suggest you take your own advice, and re-read your posts before you post them. Your post IMO is very misleading.

    A default constructor is already pre-defined regardless (if there are none defined), unless you have an overloaded one I believe.
    Look at those highlighted sections, to me you're saying you really aren't sure and are guessing. Even that "(if there are none defined) is misleading. What is that "none" you're talking about, the default no argument constructor or all constructors?

    Jim

  10. #10
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Quote Originally Posted by jimblumberg View Post
    I suggest you take your own advice, and re-read your posts before you post them. Your post IMO is very misleading.


    Look at those highlighted sections, to me you're saying you really aren't sure and are guessing. Even that "(if there are none defined) is misleading. What is that "none" you're talking about, the default no argument constructor or all constructors?

    Jim
    I'm confident that the majority would find it clear enough. I proofread all of my posts. You still quoted the only part relevant to proving your own side, and completely ignored the most critical part of my post, making an assumption without asking the questions if you weren't clear on what I was saying. You can cherry-pick the small irrelevancies without looking at the big picture of a post all you want, but what you're essentially trying to debate here is not much different than saying that a green and a red apple aren't the same fruit because they are a different color.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,506
    I think the problem with is the use of "regardless" with two exceptions: if it were true "regardless", then there are no exceptions. But obviously the reasonable way to resolve this contradiction is to suppose that the word "regardless" was not used correctly and hence should be ignored.
    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

  12. #12
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Should I alter my sentence to suit both of your ego's then since I didn't realize that this forum was supposed to be about English Grammar? :S

    A default constructor is already pre-defined regardless (assuming there are none defined)
    There you go. My god.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,506
    Quote Originally Posted by cstryx
    Should I alter my sentence to suit both of your ego's then since I didn't realize that this forum was supposed to be about English Grammar?
    I was actually pointing out that your sentence is quite easy to correctly interpret despite the incorrect use of the word "regardless", but apparently your ego does not permit you to acknowledge that.

    EDIT:
    No, your fix is still wrong: replacing "if" with "assuming" without removing "regardless" retains the contradiction/oxymoron. But don't bother: anyone who was somehow confused by that would already have doubts clarified by the time he/she reads post #12.
    Last edited by laserlight; 08-31-2014 at 09:22 AM.
    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

  14. #14
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Quote Originally Posted by laserlight View Post
    I was actually pointing out that your sentence is quite easy to correctly interpret despite the incorrect use of the word "regardless", but apparently your ego does not permit you to acknowledge that.

    EDIT:
    No, your fix is still wrong: replacing "if" with "assuming" without removing "regardless" retains the contradiction/oxymoron. But don't bother: anyone who was somehow confused by that would already have doubts clarified by the time he/she reads post #12.
    If not already by post #8 to show that this is not what I meant. I'm going to continue to ignore all of the senseless cherry picking that you do in the future however, there's no point in bothering with it, since I'm only interested in this forum for programming. Regardless if you are correct or not, I'll let both of you continue to argue that "red vs green apple" analogy while I can just agree that an apple is an apple and get along with eating that apple instead of debating what it is and stalling time so to speak... Ego has nothing to do with carelessness--I'm not here to become a grammar teacher, and lots of people put notes inside of brackets that pertain to that part of the text, which ARE NOT supposed to be part of the original text, even though that might not be grammatically correct. Would you like me to explicitly define a "NOTE:" in the beginning and italicize it now? :S

    Both of you seem more concerned about my grammar here instead of accepting initially that this was not what I meant. As I said, I have rarely had this issue with that style of syntax for my writing. You might also want to understand that not all people here are fluently English.
    Last edited by cstryx; 08-31-2014 at 09:57 AM.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,506
    Quote Originally Posted by cstryx
    If not already by post #8 to show that this is not what I meant.
    Exactly.

    Quote Originally Posted by cstryx
    I'm going to continue to ignore all of the senseless cherry picking that you do in the future however, there's no point in bothering with it, since I'm only interested in this forum for programming.
    You don't get it, do you? I was supporting you by pointing out that a reader should have been able to determine what you meant without further clarification, even though there was an extra word that could theoretically confuse a reader.

    If this is "senseless cherry picking", then the correct assertion is that your statement contained a contradiction and thus is simply wrong, which was jimblumberg's point in post #6. Since you disagree, then you should agree that this is not "senseless cherry picking".

    Quote Originally Posted by cstryx
    Regardless if you are correct or not, I'll let both of you continue to argue that red vs green apple while I can just agree that an apple is an apple and get along with eating that apple instead of debating what it is and stalling time so to speak.
    Now this is correct use of "regardless"
    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

Similar Threads

  1. Class FILE to write in DOS encoding.
    By ArnoldRich in forum C Programming
    Replies: 5
    Last Post: 12-13-2013, 10:30 AM
  2. no default constractor ....
    By mixalissen in forum C++ Programming
    Replies: 11
    Last Post: 05-17-2008, 04:15 AM
  3. How write class like vector that deletes self?
    By 6tr6tr in forum C++ Programming
    Replies: 7
    Last Post: 05-04-2008, 11:46 AM
  4. I am trying to write a code to search a class
    By jrb47 in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2006, 02:33 PM
  5. learn to write a class
    By henry in forum C++ Programming
    Replies: 14
    Last Post: 04-10-2003, 03:31 PM