Click here to Skip to main content
15,884,933 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi,

I have come across a code when I was reading a C++ primer which looks the following:
class MyIntArray {
  MyIntArray(int size = DefaultSize){}
 private:
 const static DefaultSize = 12;
};

My doubts are the followng:
->Its mentioned in the book that it is a default constructor,but how can it have an argument?
->How does that code in parenthesis work?

Thaks in advance.
Posted

To add to what Manfred said, it is a default constructor: it is just a default constructor with a optional argument. If you omit the argument, it is a default constructor. It you don't, it is a parametrised constructor.

Any parameter in C++ can be optional, provided there are no non-optional parameters to the right of it. All you have to do it provide the default value as part of the function declaration.
MSDN[^] has a reasonable explanation.
 
Share this answer
 
IBM:
A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values.


http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr376.htm[^]
 
Share this answer
 
Comments
hakz.code 20-May-11 0:44am    
Thank you for the link,it cleared all my doubts about this constructor.
Albert Holguin 20-May-11 9:47am    
you're welcome :)
While your questions have been thoroughly answered above, I'd like to point out that if this code is a literal transcript of that primer you are reading, you'd best discard it at once and get some decent book or other online source:

1. You can't define a const value right inside the class definition. If that definition is inside a header and that header may be included from multiple locations, as is normally the case. Defined like that, you'll get linker errors.

2. Not only the const, but also the constructor is defined private. I doubt that was the intention.

3. I would expect the constructor to do some trivial initialization, using the argument. Instead it does nothing, raising questions about the validity and purpose of that code example.

4. I'd expect a decent primer to explain what default arguments are if it uses them in example code. Judging by your question it doesn't.

It's not that I'm expecting a full-fledged introduction into C++ from a simple primer, but it seems wrong to provide examples that wouldn't even compile & link. How can you get a good impression of the functionality and capabilities of C++ if the explanations are plain wrong?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-May-11 16:57pm    
Correct. My 5.
--SA
hakz.code 20-May-11 0:55am    
Hi Stefan63
The primer I am following gives the overview of the overall C++ features first,then all the chapters are there with the detailed explanation for these stuff.
About the constructor being private,as I was typing the code by recalling what I have understood, I missed the public keyword.
But about the issue with the const value you have explained ,I hope I'll get to know about it in detail from my primer in further chapters,
Thank you
Stefan_Lang 23-May-11 4:56am    
Ok, it's good to know this wasn't taken literally from the primer.

About the const - it's the same as declaring&defining a global const in C. The declaration belongs in the header all right. But the definition (i. e. assigning the value) should be done in a source file, e. g. like this:

// file MyIntArray.h
class MyIntArray {
//...
static const int DefaultSize; //only declaration
};

// file MyIntArray.cpp
#include "MyIntArray.h"
const int MyIntArray::DefaultSize = 12; // definition
When the parameter size is omitted it will be set to DefaultSize which is twelfe (12) in this case. The code block of the constructor which you depicted is empty though which would mean it does nothing (AFAICS).

Cheers!

-MRB
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900