Like Tree1Likes
  • 1 Post By phantomotap

about constructor and class

This is a discussion on about constructor and class within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I wanted to know that if we are making a program eg.for music items. Can we write the ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    about constructor and class

    Hi guys,

    I wanted to know that if we are making a program eg.for music items.

    Can we write the class as:

    Code:
    class Music_item
    
    {
    
    private:
    
    int song;
    
    public:
    
    Music_item(); ----> default constructor
    
    
    };
    ---------------------------------------------------------
    Will their be any problem if we write the class and its constructor with an underscore?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351
    that appears to be valid code. without knowing your requirements, it's hard to know if will be adequate to do what you want it to do.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,422
    If your constructor has no user defined code is there any requirement for it? As far as i recall I I have always written a declaration as a matter of course, but i understood that c++ would just initialise everything to binary 0 if no constructor was typed by the coder. As a backward compatability step with C code constructs, Does that still apply in the new standard?
    Last edited by rogster001; 06-20-2012 at 03:25 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    227
    Quote Originally Posted by rogster001 View Post
    As far as i recall I I have always written a declaration as a matter of course, but i understood that c++ would just initialise everything to binary 0 if no constructor was typed by the coder. As a backward compatability step with C code constructs, Does that still apply in the new standard?
    That was never the case, even before C++11. If your class contains members of built-in types or PODs, they won't get initialized to anything unless you make it so by supplying a user-defined constructor in which you explicitly initialize those members.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    Will their be any problem if we write the class and its constructor with an underscore?
    The only time you would ever have a problem with an underscore in a variable, function name would be if it was a leading underscore. Many of the standard library functions use underscores. See this documentation for std::string and you should notice functions such as c_str(), etc.


    Jim

  6. #6
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    227
    Quote Originally Posted by jimblumberg View Post
    The only time you would ever have a problem with an underscore in a variable, function name would be if it was a leading underscore. Many of the standard library functions use underscores. See this documentation for std::string and you should notice functions such as c_str(), etc.


    Jim
    The standard reserves GLOBAL identifiers with a LEADING underscore for use by compiler vendors. All identifiers with 2 consecutive underscores are also reserved. A single underscore in the middle of an identifier is never a problem.

    EDIT: I just reread your post and realized that you never actually implied anything to the effect of the boldened part. Sorry, reading comprehension fail on my part.
    Last edited by antred; 06-20-2012 at 03:45 PM.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,214
    The standard reserves GLOBAL identifiers with a LEADING underscore for use by compiler vendors.
    This is true, but in practice you need to avoid defining any labels with a leading underscore.

    You see, a lot of standard library implementations interpreted that in such a way that it allowed macros at global scope of that form to be reserved.

    In other words, a lot of standard library implementations define macros which don't honor scope so the notion that you can use labels of that form in a smaller scope is flawed.

    Soma
    antred likes this.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    The standard reserves GLOBAL identifiers with a LEADING underscore for use by compiler vendors. All identifiers with 2 consecutive underscores are also reserved. A single underscore in the middle of an identifier is never a problem.
    And you're telling me this for? As I said the only time you would ever have a problem is when you use a leading underscore. I didn't say when because it doesn't really matter in this context. In my opinion you should avoid using a leading underscore for any variable, I don't care where you use it I consider using a leading underscore a bad practice.


    Jim

  9. #9
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    227
    Quote Originally Posted by jimblumberg View Post
    And you're telling me this for?
    Read my edit.

  10. #10
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,422
    Quote Originally Posted by antred View Post
    That was never the case, even before C++11. If your class contains members of built-in types or PODs, they won't get initialized to anything unless you make it so by supplying a user-defined constructor in which you explicitly initialize those members.
    Ok so forget the initialisation, although that has been written in published books - no guarantee i know, but regardless, I mean the fact you can / could instansiate an object without writing a constructor declaration in the class declaration
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,214
    could instansiate an object without writing a constructor declaration in the class declaration
    This still holds; if you don't write a default constructor the compiler will provide one.

    Soma

  12. #12
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,422
    Quote Originally Posted by phantomotap View Post
    This still holds; if you don't write a default constructor the compiler will provide one.

    Soma
    Exactly - like a solictor in a legal case...!
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,214
    O_o

    Did you ask that just to setup a really bad joke?

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-31-2011, 01:23 AM
  2. Replies: 40
    Last Post: 06-02-2010, 10:08 AM
  3. using a class constructor w/ class array
    By matt000r000 in forum C++ Programming
    Replies: 4
    Last Post: 11-01-2009, 06:08 AM
  4. Replies: 9
    Last Post: 06-20-2008, 02:41 AM
  5. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 12:47 PM

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