473,657 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is there a difference between new MyClass; and new MyClass();?

MyClass *p = new MyClass;
MyClass *p = new MyClass();

do they mean something different?

Thanks a lot,
Mario Fratelli.
Jul 22 '05 #1
32 2719
Hi,

"Mario Fratelli" <si**@yahoo.com > wrote in message
news:e4******** *************** ***@posting.goo gle.com...
MyClass *p = new MyClass;
MyClass *p = new MyClass();

do they mean something different?
No, but note that:

MyClass p;
and
MyClass p();

does mean something different. The first is creating an object of MyClass.
The second is prototyping a function p() returning MyClass;
Regards, Ron AF Greve

Thanks a lot,
Mario Fratelli.

Jul 22 '05 #2
On 7 Dec 2003 07:27:00 -0800, si**@yahoo.com (Mario Fratelli) wrote:
MyClass *p = new MyClass;
MyClass *p = new MyClass();

do they mean something different?


Yes, they do.

If MyClass is a POD (Plain Old Datastructure), roughly, no virtual
functions and no user-defined constructors, then with a standard-
conforming compiler the first statement creates a new instance without
initializing it, whereas the second gives default-initialization,
which for POD fields is zeroing.

If MyClass is not a POD then the two statements are equivalent.

Jul 22 '05 #3
> MyClass *p = new MyClass;
MyClass *p = new MyClass();

do they mean something different?


Yes: In the first example, p points at an object that is
default-initialized; in the second case, p points at an object that is
value-initialized.

Default- and value-initialization differ only in the case where MyClass does
not have a user-defined constructor (although it might have data members of
classes that themselves have user-defined constructors). For example:

class MyClass {
public:
std::string name, address;
int postalcode;
};

In both examples above, the initial values of p->name and p->address will be
empty strings. However, in the first case, p->postalcode will be undefined,
whereas in the second case (p = new MyClass()), p->postalcode will be zero.

Note that this behavior is a change from C++98; many compilers do not yet
implement this behavior correctly.
Jul 22 '05 #4
Mario Fratelli wrote in
news:e4******** *************** ***@posting.goo gle.com:
MyClass *p = new MyClass;
MyClass *p = new MyClass();

do they mean something different?


Yes, but only in the case where MyClass *doesn't* have any constructors,
particularly a default ctor.

If so in the second case, "value initialization" (IIRC) is used,
which means all of MyClass members are initialized by "value
initialization" , a recursive thing that ultimatly leads to the members
default constructors being called or in the case of inbuilt types
they are 0 initialized.

Note that for this to work all of MyClass 's members must have *no* user
defined constructors *or* they must also have a user defined default
constructor, this rule applies recusivly too.

example (simple):

class MyClass
{
public:

int i;
};

MyClass *initialized = new MyClass();
MyClass *not_initialize d = new MyClass;

intialized->i == 0;
not_intialized->i == /* un-initialized (AKA garbage) */;

not_intialized->i should be initialized before it is value is used or
the programme will exhibit undefined behaviour.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5
"Moonlit" <al******@jupit er.universe> wrote...
"Mario Fratelli" <si**@yahoo.com > wrote in message
news:e4******** *************** ***@posting.goo gle.com...
MyClass *p = new MyClass;
MyClass *p = new MyClass();

do they mean something different?
No


Wrong. Depending on the 'MyClass', there may be a difference.
, but note that:

MyClass p;
and
MyClass p();

does mean something different. The first is creating an object of MyClass.
The second is prototyping a function p() returning MyClass;
Regards, Ron AF Greve

Thanks a lot,
Mario Fratelli.


Jul 22 '05 #6
"Andrew Koenig" <ar*@acm.org> wrote in message news:<P1******* *************** *@bgtnsc04-news.ops.worldn et.att.net>...
Note that this behavior is a change from C++98; many compilers do not yet
implement this behavior correctly.


since the behaviour is differente only when you don't have default
constructors, how can I test the compiler?

Mario Fratelli.
Jul 22 '05 #7
"Mario Fratelli" <si**@yahoo.com > wrote...
"Andrew Koenig" <ar*@acm.org> wrote in message

news:<P1******* *************** *@bgtnsc04-news.ops.worldn et.att.net>...
Note that this behavior is a change from C++98; many compilers do not yet implement this behavior correctly.


since the behaviour is differente only when you don't have default
constructors, how can I test the compiler?


It's rather difficult. It would involve using an uninitialised values
of POD, which in itself can cause undefined behaviour.

But try this:

struct A {
int a;
};
#include <iostream>
int main() {
char *storage = new char[sizeof(A)];
for (int i = 0; i < sizeof(A); ++i)
storage[i] = 1;
A *pa = new (storage) A;
std::cout << pa->a << std::endl;
pa = new (storage) A();
std::cout << pa->a << std::endl;
delete[] storage;
}

If you get two zeroes as output, your compiler value-initialises
PODs when it is supposed to leave it uninitialised.

Make sure to build a release version as debug ones often initialise
values to 0 without your specific actions.

Victor

P.S. I tested this with VC++ v6sp5 (and it failed to default-
initialise the 'A' the second time) and with Intel C++ v4.5, which
gave the expected output:

16843009
0

(well, the first value is not something specifically expected, just
so it is not 0)
Jul 22 '05 #8

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:rcJAb.4578 99$Fm2.450221@a ttbi_s04...
"Moonlit" <al******@jupit er.universe> wrote...
"Mario Fratelli" <si**@yahoo.com > wrote in message
news:e4******** *************** ***@posting.goo gle.com...
MyClass *p = new MyClass;
MyClass *p = new MyClass();

do they mean something different?
No


Wrong. Depending on the 'MyClass', there may be a difference.


Oops, your right.

Sorry for my bad advice :-(
, but note that:

MyClass p;
and
MyClass p();

does mean something different. The first is creating an object of MyClass. The second is prototyping a function p() returning MyClass;
Regards, Ron AF Greve

Thanks a lot,
Mario Fratelli.



Jul 22 '05 #9
On Sun, 07 Dec 2003 15:53:16 GMT, al***@start.no (Alf P. Steinbach) wrote:
On 7 Dec 2003 07:27:00 -0800, si**@yahoo.com (Mario Fratelli) wrote:
MyClass *p = new MyClass;
MyClass *p = new MyClass();

do they mean something different?


Yes, they do.

If MyClass is a POD (Plain Old Datastructure), roughly, no virtual
functions and no user-defined constructors, then with a standard-
conforming compiler the first statement creates a new instance without
initializing it, whereas the second gives default-initialization,
which for POD fields is zeroing.

If MyClass is not a POD then the two statements are equivalent.


Among 112 spams and Swen viruses a mail from Andrew Koenig, almost deleted
in my spam-removal frenzy, pointing my attention to that last sentence.

Ooops.

And thanks, Andrew.

While the sentence holds wrt. C++ 1998, the C++ 2003 standard revision changed
the rules slightly. The relevant paragraps are §5.3.4/15, which defines the
effect with and without "()", and §8.5/5, which defines the terms
"zero-initialization" , "default-initialization" and, in C++ 2003, the new term
"value-initialization" . In C++ 2003, the 'new' statement with "()" gives
value-initialization, which is not necessarily equivalent to
default-initialization: roughly, value-initialization only calls the default
constructor if it is a user-defined constructor, and otherwise gives
zero-initialization. So "()" can no longer be read as "call constructor".

The upshot is that in C++ 2003 you're guaranteed zero-initialization in at
least one more case than in C++ 1998, namely in the case of "new T()" where T
is non-POD and does not have a user-defined constructor.

This language keeps getting subtler and subtler.

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2524
by: modemer | last post by:
If I define the following codes: void f(const MyClass & in) {cout << "f(const)\n";} void f(MyClass in) {cout<<"f()\n";} MyClass myclass; f(myclass); Compiler complain that it can't find the best match. Anyone could give a detail explanation in theory? Which one is good?
5
1500
by: Gunnar G | last post by:
What is the difference between the two lines in the main function? class MyClass{ ....}; int main(){ MyClass a(); MyClass a; }
1
2584
by: Iced Crow | last post by:
What is the difference between using MyClass and Me inside of inherited classes?
7
1408
by: guy | last post by:
Stirring up trouble here;) why is it that C# programmers try and denigrate VB.NET while VB.NET developers seem to have no problem with C# but just prefer VB.NET? I use both and this generally seems to be the attitude, not with everyone obviously!
15
6004
by: Youssef Mesri | last post by:
What is the difference between these two situations: 1- hold a namespace which contains justs some functions: namespace MyNamespace { void foo1(); void foo2(); void foo3(); void foo4();
4
1323
by: Radu | last post by:
Hi. I have the following question: Since the methods are virtual, the following code produces "Child other stuff" - the call runs as if pasted in Child... it runs in Parent but in the context of Child. ------------------------------- Module Module1 Sub Main() Dim obj As New Child()
4
6950
by: 'Mani | last post by:
Hi, This is just a generic question, where i want to know what is the difference in including a header file in a .h file and .cpp file. I have a class called MyClass (MyClass.h & MyClass.cpp). There is another class (OtherClass.h & OtherClass.cpp) OtherClass.cpp has a forward declaration to a class called 'Calc' which is in the namespace called 'Utils' like below:
2
1355
by: shapper | last post by:
Hello, I want to redefine the way an <atag behaves. I saw to approaches: .myClass a { ... } and
9
6985
by: Stephan Steiner | last post by:
Hi I seem to have a bit of trouble understanding one bit of how generics work: In C#, every class automatically derives from object, and inherits a bunch of properties (i.e. ToString()). Thus, (MyClass is object) should always evaluate as true. However, if I have a method with the following signature:
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7333
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6167
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
5632
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
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2731
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

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.