473,725 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"new" vs "new class"

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
6 2516
"dpr" <vi********@vir tualway.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
On Thu, 12 Feb 2004 12:25:17 +0100, "Ivan Vecerina" <se*********@ve cerina.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
Ivan Vecerina wrote:
"dpr" <vi********@vir tualway.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
Ivan Vecerina wrote:
"dpr" <vi********@vir tualway.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
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c0******** *****@news.t-online.com...
Ivan Vecerina wrote:
"dpr" <vi********@vir tualway.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
lallous wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c0******** *****@news.t-online.com...
Ivan Vecerina wrote:
> "dpr" <vi********@vir tualway.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
2866
by: Rv5 | last post by:
Rookie c++ question, but Ive spent the last 5 years doing Java, where everytime I created an object I used new. In c++ I can create my objects without and its confusing me just a little. I have a class called polynomial. Its a nothing little class right now, with just int variables, a basic container class. Im using it as I go through some tutorials, but in this particular tutorial its telling me to do polynomial *first = new...
8
13302
by: Dot net work | last post by:
I need VB.NET's "shadows" functionality inside a C# project. I tried the "new" keyword, but it didn't seem to work, because my particular function does in fact differ in signature to the function that is being hidden in the base class. In VB.NET, the shadows keyword hides all the inherited functions, regardless of signature. I need this functionality in C#.
38
2049
by: looping | last post by:
For Python developers around. >From Python 2.5 doc: The list of base classes in a class definition can now be empty. As an example, this is now legal: class C(): pass nice but why this syntax return old-style class, same as "class C:", and not the new style "class C(object):" ?
24
2832
by: M O J O | last post by:
Hi, Instead of doing this.... Public Class Form1 Public Shared Sub CreateAndShow() Dim f As New Form1 f.Show() End Sub
37
3970
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as a function
14
5686
by: mlw | last post by:
Do not take anything about this, it is not a flame or troll, while I'm not new to Java I favor C++. However, I may need to use it in a contract position, and am concerned that the restrictions it places on application code. Take, for instance, this C++ construct: class foo { char *m_name;
11
1769
by: letz | last post by:
Hi, We have a class whose objects are to be allocated in shared memory. To do that a ShmManager base class is defined so that operator new and delete are redefined to allocate segments in shared memory. A typical class "Foo" then inherit from ShmManager to have get this behaviour. class ShmManager {
12
4822
by: Robert Fuchs | last post by:
Hello, This example: public class BaseC { public int x; public void Invoke() {} } public class DerivedC : BaseC
30
3833
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at the end of the app for example: class test { public: int x;
0
8889
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.