Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I multi-declare a class

Tags:

c++

I can do this

extern int i;
extern int i;

But I can't do the same with a class

class A {
..
}
class A {
..
}

While in both cases no memory is being allocated.

like image 888
deeJ Avatar asked Feb 04 '10 08:02

deeJ


People also ask

Can a variable have multiple declarations in C?

Example - Declaring multiple variables in a statementIf your variables are the same type, you can define multiple variables in one declaration statement. For example: int age, reach; In this example, two variables called age and reach would be defined as integers.

Can you declare a variable more than once?

Declaring multiple variables in a single declaration could cause confusion about the types of variables and their initial values. In particular, do not declare any of the following in a single declaration: Variables of different types.

What is multiple declaration in C?

E2238 Multiple declaration for 'identifier' (C++)A function declared in two different ways. A label repeated in the same function. Some declaration repeated, other than an extern function or a simple variable.

Can a variable have two types Java?

There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (§4.2) and reference values (§4.3).


1 Answers

The following are declarations:

extern int i;
class A;

And the next two are definitions:

int i;
class A { ... };

The rules are:

  • a definition is also a declaration.
  • you have to have 'seen' a declaration of an item before you can use it.
  • re-declaration is OK (must be identical).
  • re-definition is an error (the One Definition Rule).
like image 102
Henk Holterman Avatar answered Oct 02 '22 19:10

Henk Holterman