Solved

Parenthesis in exeption constructors/class constructors

Posted on 2003-11-25
2
212 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 how to clear a vector as well as how to detect empty vectors in C++.

867 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

24 Experts available now in Live!

Get 1:1 Help Now