Solved

Parenthesis in exeption constructors/class constructors

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

Suppose I have a very very simple program:

#include <iostream>

using namespace std;

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

class ExceptionClass
{
public:
      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();

OR

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.
0
Comment
Question by:mrwad99
2 Comments
 
LVL 15

Accepted Solution

by:
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.

--efn
0
 
LVL 19

Author Comment

by:mrwad99
ID: 9823408
Marvelous.  Many thanks !
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now