Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ClassA a; vs. ClassA a();

Posted on 2014-01-26
13
Medium Priority
?
225 Views
Last Modified: 2014-01-30
What's the difference between
ClassA a;
ClassA a();

Open in new window

0
Comment
Question by:deleyd
[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
  • 6
  • 3
  • 2
  • +2
13 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39810485
In short: There is none. Both instantiate an instance of 'ClassA' via the default constructor. The 2nd line could be called more explicit, yet the brackets could as well be called superfluous.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 39810490
The first constructs an object called a.  The second declares a function called a.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 39810496
>>In short: There is none.
The standard disagrees.  Due to the "most vexing parse"  rule, the second is a function declaration.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:evilrix
ID: 39810498
0
 
LVL 32

Expert Comment

by:phoffric
ID: 39810552
ClassA * aptr = new ClassA();

In the above case, the parens are superfluous. Either way, you get the default constructor.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39810957
>> Due to the "most vexing parse"  rule, the second is a function declaration.

Even within a function's body? ;o)
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 200 total points
ID: 39810998
Here is how to (1) remove the error from the "most vexing parse" problem in the link; and (2) how to force the TimeKeeper constructor to be called, rather than treating time_keeper as a function declaration.
class Timer {
 public:
	 Timer() { cout << "Timer" << endl;};
};
 
class TimeKeeper {
 public:
	 TimeKeeper(const Timer& t) {cout << "TimeKeeper" << endl;};
	 int get_time() {cout << "get_time" << endl; return 1;};
};
 
int main() {
  TimeKeeper time_keeper((Timer()));
  return time_keeper.get_time();
}

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 39811001
oops - I just saw that the link mentioned this. So, never mind.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 39811159
>Even within a function's body? ;o)

Yes,  anywhere.

NB. You can forward declare a function within the scope of another function. This limits the scope to the function into which the other function is declared.
0
 
LVL 1

Expert Comment

by:eeeabhi
ID: 39811340
ClassA a;//Create an instance of Object type ClassA with the instance name a
ClassA a();//Declare a function which returns ClassA type object and takes in no arguments.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 39811356
eeeabhi, we have already established that.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 200 total points
ID: 39811383
>> ClassA * aptr = new ClassA();
>> In the above case, the parens are superfluous. Either way, you get the default constructor.

Actually, that's also not true. In the case of POD or intrinsic types, not calling the synthesized constructor explicitly leaves the object uninitialised. Calling it explicitly constructs them with default values.

int * p = new int

is not the same as

int * p = new int()

The same is true for user defined PODs.

#include <iostream>

struct foo
{
   int bar;
};

int main()
{
   // aggregate members are not default initialised
   foo const * f1 = new foo;

   // aggregate members are default initialised
   foo const * f2 = new foo();

   std::cout
         << f1->bar << std::endl // output is indeterminate
         << f2->bar << std::endl; // output will be 0 (same as int()).

   delete f1;
   delete f2;
}

Open in new window


More specifically, the standard says...



To value-initialize an object of type T means:
— if T is a (possibly cv-quali¿ed) class type (Clause 9) with a user-provided constructor (12.1), then the
default constructor for T is called (and the initialization is ill-formed if T has no accessible default
constructor);
— if T is a (possibly cv-quali¿ed) non-union class type without a user-provided constructor, then the object
is zero-initialized and, if T’s implicitly-declared default constructor is non-trivial, that constructor is
called.
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized.

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

To default-initialize an object of type T means:
— if T is a (possibly cv-quali¿ed) class type (Clause 9), the default constructor for T is called (and the
initialization is ill-formed if T has no accessible default constructor);
— if T is an array type, each element is default-initialized;
— otherwise, no initialization is performed.

If no initializer is speci¿ed for an object, the object is default-initialized; if no initialization is performed, an
object with automatic or dynamic storage duration has indeterminate value.


And with regards to the "most vexing parse" rule, the standard states...


Note: Since () is not permitted by the syntax for initializer, X a(); is not the declaration of an object of class X, but the declaration of a function taking no argument and
returning an X.
0
 

Author Closing Comment

by:deleyd
ID: 39822749
Most vexing. This C++ syntax will drive me nuts.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

721 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