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
Solved

Parenthesis in exeption constructors/class constructors

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cross compile release version of c++ program for linux 2 136
thread-safe code in c++ 2 115
Installshield for Embarcadero EX 10.1 Berlin 4 60
Need some help with mailto 16 20
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

809 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