By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,589 Members | 1,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,589 IT Pros & Developers. It's quick & easy.

"new" vs "new class"

P: n/a
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.

Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"dpr" <vi********@virtualway.com.br> wrote in message
news:c0**********@news-reader4.wanadoo.fr...
| 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Jul 22 '05 #2

P: n/a
On Thu, 12 Feb 2004 12:25:17 +0100, "Ivan Vecerina" <se*********@vecerina.com> wrote:
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".

Jul 22 '05 #3

P: n/a
Ivan Vecerina wrote:
"dpr" <vi********@virtualway.com.br> wrote in message
news:c0**********@news-reader4.wanadoo.fr...
| 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.

Jul 22 '05 #4

P: n/a
Ivan Vecerina wrote:
"dpr" <vi********@virtualway.com.br> wrote in message
news:c0**********@news-reader4.wanadoo.fr...

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
Jul 22 '05 #5

P: n/a
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c0*************@news.t-online.com...
Ivan Vecerina wrote:
"dpr" <vi********@virtualway.com.br> wrote in message
news:c0**********@news-reader4.wanadoo.fr...
| 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.

Rolf, you mean something like forward declaration? As if:

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

--
Elias
Jul 22 '05 #6

P: n/a
lallous wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c0*************@news.t-online.com...
Ivan Vecerina wrote:
> "dpr" <vi********@virtualway.com.br> wrote in message
> news:c0**********@news-reader4.wanadoo.fr...
> | 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.

Rolf, you mean something like forward declaration? As if:

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


Exactly.

Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.