2

Which best describes the following:

A a();
A a;

Where "A" is a generic class? I've always believed you were initialising an object for the class, but, others seem to think of instantiation - Something I've only really come across in VB.net.

Thoughts please? :)

John
  • 7,301
  • 2
  • 16
  • 23
Phorce
  • 4,424
  • 13
  • 57
  • 107
  • 6
    Neither. It is a function declaration. – juanchopanza Oct 10 '13 at 21:00
  • My C++ is pretty rusty, but I believe it's declaring and initializing a automatic variable "a" of type "A". – Hot Licks Oct 10 '13 at 21:03
  • You initialize a variable, but objects are instantiated. [http://stackoverflow.com/questions/2330767/what-is-the-difference-between-instantiated-and-initialized][1] [1]: http://stackoverflow.com/questions/2330767/what-is-the-difference-between-instantiated-and-initialized – user2860325 Oct 10 '13 at 21:05
  • There's no such thing as a "generic class" in C++. Classes in C++ are types, and types are always specific. – Kerrek SB Oct 10 '13 at 21:05
  • @HotLicks no, it is declaring a parameter-less function `a` that returns an `A`. C++ is a bit strange sometimes. – juanchopanza Oct 10 '13 at 21:05
  • (For `A a;` which was intended) This is highly dependent on location and some other context. I could also see the words "declaration" and "definition" entering contention. – John Oct 10 '13 at 21:40
  • @juanchopanza - OK, then it's the other way -- `A a(x);` is declaring and initializing the automatic variable. – Hot Licks Oct 10 '13 at 22:31

3 Answers3

6
A a(); 

It declares a function which returns A type. This is a surprise to many users. This is related to the Most vexing parse.

In the following two examples, the intent is obvious

A a(3);    // an object a, of type A, initialized with 3
A a(int);  // a *function* a, taking an int parameter and returning A

But when the brackets are empty, the default is a function declaration. If you want a variable, you should remove the ();

A a;
Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
billz
  • 44,644
  • 9
  • 83
  • 100
0

This is a Declaration for most of the cases. This declares a function named a which returns type A and takes no arguments.

Since A could be a macro or something weird, I would not say it is always a declaration.

CS Pei
  • 10,869
  • 1
  • 27
  • 46
  • What does "for most of the cases if A is type" mean?! What *else* could `A` possibly be? – Kerrek SB Oct 10 '13 at 21:08
  • 1
    Sneaky, but true :-) Though if any token in the question could be a macro, then it's hard to say *anything*. The code could be a bittorrent client, for all we know. – Kerrek SB Oct 10 '13 at 21:09
  • In fact, I'd go as far as assuming that for a general C++ question on SO, none of the tokens that aren't explicitly shown to be macros are in fact macros, since that would really just make *every* question completely unanswerable. – Kerrek SB Oct 10 '13 at 21:12
0

Assuming that your complete program is:

class A
{
};

A a;

Then the answer is "both".

C++ has terminology that is not necessarily the same as that of other languages. Using the C++ terminology, the declaration A a declares an object a of type A.

This declaration is also a definition. The type A is required to be complete.

[Note: If the type A was a template specialization (e.g. typedef X<int> A), that specialization would be implicitly instantiated.]

In the C++ terminology, initialization means to give an initial value for an object. In the example, the object a will be default-initialized, which may result in no actual initialization being performed.

The dictionary definition of the term "instantiation" is an appropriate way to describe this declaration. The declaration "instantiates" an object a, which is an "instance" of the class A.

willj
  • 2,991
  • 12
  • 24