Solved

Parenthesis in exeption constructors/class constructors

Posted on 2003-11-25
2
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.

756 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