Parenthesis in exeption constructors/class constructors

Posted on 2003-11-25
Last Modified: 2010-04-01
Ah hello.

Suppose I have a very very simple program:

#include <iostream>

using namespace std;

class NormalClass
      NormalClass() { cout << "NormalClass\n"; }

class ExceptionClass
      ExceptionClass() { cout << "ExceptionClass\n"; }

int main()
      NormalClass a(); <----------------(1)
      try {
           throw (ExceptionClass()); <---(2)
      } catch (const ExceptionClass& ex) {
           cout << "Caught!\n";
      return 0;

Now, when I leave the code as-is, I do not get the call to NormalClass being caried out.  This is proven by the lack of "NormalClass" in the output.  However, when I create a new ExceptionClass object, as in (2), with exactly the same semantics, *including the parentheses*, I get the output "ExceptionClass", showing that the constructor is being called.

If I remove the parentheses in (1) changing the line to

NormalClass a;

the output is as excepted.  So,

1) I know that if I leave the () off, the compiler assumes it is a function definition, but that does not explain why I can leave the parameters off or add them on (the output being the same either case) if I state

NormalClass* a = new NormalClass();


NormalClass* a = new NormalClass;

And which is better form ?  No parentheses or parentheses ?

2) What is the crack with exceptions then ?  Why do I need to include them when creating an exception class object to be thrown ?

Thanks in advance.
Question by:mrwad99
LVL 15

Accepted Solution

efn earned 50 total points
ID: 9820987
When you declare a, you are saying "let there be a."

NormalClass a;

says "let there be a NormalClass named a."

NormalClass a(0);

says "let there be a NormalClass named a and initialize it with 0."

NormalClass a(int);

says "there is a function a that takes an int parameter and returns a NormalClass."

NormalClass a();

To be consistent with the interpretations above, this could mean either "let there be a NormalClass a and give it default initialization" or "there is a function a that takes no parameter and returns a NormalClass."  The syntax rules specify the second interpretation.

The operand of the new operator is not a declaration.  The object the new operator creates does not have a name.  The operand specifies the type of the object to create and optionally specifies an initializer for the object.

NormalClass* a = new NormalClass;

creates an unnamed NormalClass with default initialization.

NormalClass* a = new NormalClass(0);

creates an unnamed NormalClass and initializes it with 0.

NormalClass* a = new NormalClass();

also creates an unnamed NormalClass with default initialization.  Because the parentheses follow the typename "NormalClass" and not an identifier like "a", it doesn't look like a function declaration, not that a function declaration could follow "new" anyway.

I personally prefer the form without parentheses for default initialization, but that's just a matter of taste, not function.

The new operator expects a type and possibly an initializer.  A throw statement expects an object.  When you write "NormalClass()" as an operand to new, you are not creating a temporary NormalClass object to be the operand, you are telling the new operator how to construct an object.  When you write "NormalClass()" as an operand to throw, you are creating a temporary object of class NormalClass with default initialization.  The parentheses are necessary to tell the compiler that you want a temporary object; if you just wrote "NormalClass", you would be referring to the NormalClass type, not an object.

LVL 19

Author Comment

ID: 9823408
Marvelous.  Many thanks !

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question