Thread: New to programming - Confusion with a bit of code

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    2

    New to programming - Confusion with a bit of code

    Hi I'm currently reading the C++ Guide for Dummies

    Anyway right now I'm working with pointers and classes, and when I create a new pointer for my class it looks like this...

    Pen *Pointerpen = new Pen;

    But in the book they threw in this...

    Pen *Pointerpen = new Pen();

    Can you actually designate memory space for a function? Or was this a typo on their part? It's never come up before and they didn't explain it.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    new Pen or new Pen() make a call to the class "constructor" function.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    2
    So it's the same exact thing?

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    13
    Yes, when you don't have any arguments to a constructor, the parens are optional and are implicitly stated.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    I would say it is a typo because they should have explained that difference, it might have been overlooked by a proofreader perhaps and the author had simply written it on auto.
    Last edited by rogster001; 08-03-2013 at 01:04 AM.
    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'"

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There is a difference in technicality, though whether it results in an actual difference depends on what is Pen.

    This default initialises the Pen object that is created:
    Code:
    new Pen
    This value initialises the Pen object that is created:
    Code:
    new Pen()
    The difference between default initialisation and value initialisation is stated as:
    Quote Originally Posted by C++11 Clause 8.5 Paragraph 6
    To default-initialize an object of type T means:
    — if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
    — if T is an array type, each element is default-initialized;
    — otherwise, no initialization is performed.
    If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.
    Quote Originally Posted by C++11 Clause 8.5 Paragraph 7
    To value-initialize an object of type T means:
    — if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
    — if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T's implicitly-declared default constructor is non-trivial, that constructor is called.
    — if T is an array type, then each element is value-initialized;
    — otherwise, the object is zero-initialized.
    An object that is value-initialized is deemed to be constructed and thus subject to provisions of this International Standard applying to "constructed" objects, objects "for which the constructor has completed," etc., even if no constructor is invoked for the object's initialization.
    So, if you have defined the default constructor for Pen, then there won't be a difference.
    Last edited by laserlight; 08-05-2013 at 06:07 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code Confusion - return 0;
    By qkzoo in forum C Programming
    Replies: 3
    Last Post: 01-01-2012, 12:05 PM
  2. Simple C programming - integer confusion
    By JGeorge in forum C Programming
    Replies: 3
    Last Post: 03-26-2010, 07:56 AM
  3. 802.11 vs 802.3 programming confusion
    By StarGhost in forum Networking/Device Communication
    Replies: 14
    Last Post: 12-07-2009, 12:14 PM
  4. OK, guys, here is my code to stop confusion
    By meili100 in forum C++ Programming
    Replies: 10
    Last Post: 11-29-2007, 04:06 PM
  5. Confusion : Warning long code
    By mart_man00 in forum C Programming
    Replies: 10
    Last Post: 04-08-2003, 08:07 PM

Tags for this Thread