"new" vs "new class"

D

dpr

I have come accross a piece of C++ code with the construct:

MyClass *c = new class MyClass();

Is there a difference between this and:

MyClass *c = new MyClass();

?

Thank you.
 
I

Ivan Vecerina

| I have come accross a piece of C++ code with the construct:
|
| MyClass *c = new class MyClass();
|
| Is there a difference between this and:
|
| MyClass *c = new MyClass();
|
| ?
No.

Actually, this is not about "new" and "new class",
but about "MyClass" or "class MyClass".

In C compilers, identifiers that refer to a struct or union
need to be explicitly prefixed with the corresponding keyword:
struct Test { int x,y; };
int f( Test a ); // Error in C, ok in C++
int f( struct Test a ); // ok in C and C++

For the sake of consistency and backwards-compatibility,
C++ allows both ways for referring to a struct, union, or class:
you can also write "class MyClass" to refer to "MyClass".


hth, Ivan
 
A

Alf P. Steinbach

In C compilers, identifiers that refer to a struct or union
need to be explicitly prefixed with the corresponding keyword:
struct Test { int x,y; };
int f( Test a ); // Error in C, ok in C++
int f( struct Test a ); // ok in C and C++

For the sake of consistency and backwards-compatibility,
C++ allows both ways for referring to a struct, union, or class:
you can also write "class MyClass" to refer to "MyClass".

Just a little point: equivalent ways mostly, but not always.

In a friend declaration of a class the word "class" or "struct"
is mandatory.

Pitfall: g++ 2.95 accepts code that omits "class" or struct".
 
R

Rolf Magnus

Ivan said:
| I have come accross a piece of C++ code with the construct:
|
| MyClass *c = new class MyClass();
|
| Is there a difference between this and:
|
| MyClass *c = new MyClass();
|
| ?
No.

Actually, this is not about "new" and "new class",
but about "MyClass" or "class MyClass".

There is a difference. The latter one declares MyClass as a class, the
former doesn't. So this will compile:

int main()
{
class X* p;
}

class X
{
};

while this won't:

int main()
{
X* p;
}

class X
{
};

However, since (AFAIK) the class must be fully defined when using new,
that difference is not noticable in the OP's code.
 
C

Christoph Rabel

Ivan said:
In C compilers, identifiers that refer to a struct or union
need to be explicitly prefixed with the corresponding keyword:
struct Test { int x,y; };
int f( Test a ); // Error in C, ok in C++
int f( struct Test a ); // ok in C and C++

For the sake of consistency and backwards-compatibility,
C++ allows both ways for referring to a struct, union, or class:
you can also write "class MyClass" to refer to "MyClass".

A class may be hidden by a variable, e.g.:

class X {};

int main() {
int X;
X x; // Doesn't compile, class X is hidden
class X x; // But this works.
}

Christoph
 
L

lallous

Rolf Magnus said:
There is a difference. The latter one declares MyClass as a class, the
former doesn't. So this will compile:

int main()
{
class X* p;
}

class X
{
};

while this won't:

int main()
{
X* p;
}

class X
{
};

However, since (AFAIK) the class must be fully defined when using new,
that difference is not noticable in the OP's code.
Rolf, you mean something like forward declaration? As if:

class X;
int main()
{
X *p;
}
class X
{
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top