473,395 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

new operator detail?

classname *ptr = new classname();

Any idea why the parenthesis in the end?I thought that " new " worked just
fine with the classname alone?
Thanks in advance.
Jul 19 '05 #1
4 3483
"Giorgos P." <gp******@ee.duth.gr> wrote...
classname *ptr = new classname();

Any idea why the parenthesis in the end?I thought that " new " worked just
fine with the classname alone?


If 'classname' is a POD (plain old data), the difference is
that without parentheses the object is left uninitialised,
and with parentheses it is default-initialised. For non-POD
types it doesn't matter.

Victor
Jul 19 '05 #2
Giorgos P. wrote:
classname *ptr = new classname();

Any idea why the parenthesis in the end?I thought that " new " worked just
fine with the classname alone?


I believe that if you use the classname without parenthesis, it just
calls the default, no-argument constructor. In other words, they are
equivalent.

The parentheses syntax is required if you have a constructor that takes
arguments.

- Adam

Jul 19 '05 #3
Victor Bazarov wrote:
<snip>
If 'classname' is a POD (plain old data), the difference is
that without parentheses the object is left uninitialised,
and with parentheses it is default-initialised. For non-POD
types it doesn't matter.

Ah, right.

Ignore my other post in this thread. I was wrong.

- Adam

Jul 19 '05 #4
Giorgos P. wrote:
classname *ptr = new classname();

Any idea why the parenthesis in the end?I thought that " new " worked just
fine with the classname alone?
...


It depends on the type designated by 'classname'. Unfortunately, you
didn't provide any details about this type.

If 'classname' names a non-POD class type, the '()' makes no difference
whatsoever, i.e. 'new classname' and 'new classname()' do the same thing.

For POD types the '()' is essential - it causes the object to be
default-initialized, while 'new classname' will not initialize it at all.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #5

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

Similar topics

30
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
4
by: August1 | last post by:
I've written an interface and implementation file along with a client source file that allows the use of an overloaded subtraction operator. However, when using the program, I'm running into a...
47
by: Roger Lakner | last post by:
I often see operator implemented something like this: class Foo { ... }; class FooList { public: const Foo& operator (unsigned index) const {return array;}; Foo& operator (unsigned index) ...
16
by: Joseph Paterson | last post by:
Hello, I've created a class to store 2 dimensional matrices, and I've been trying to overload the operator, so access to the elements of the matrix is easy. I have 3 private variables, _m, _row...
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
11
by: Noah Roberts | last post by:
template < typename T > std::istream & operator >(std::istream & in, std::pair<T,T& p) { in >p.first >p.second; return in; } .... std::istream_iterator< std::pair<size_type, size_type
4
by: subramanian100in | last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit conversions", the authors have advised the use of named...
3
by: Martin T. | last post by:
Hello. I tried to overload the operator<< for implicit printing of wchar_t string on a char stream. Normally using it on a ostream will succeed as std::operator<<<std::char_traits<char> will...
5
by: Hill | last post by:
This is a program just for testing operator overloading. But I found the operator ++ doesn't act like on built-in types. For detail: When int array; Ptr_to_T<intsmart_ptr(&array, array, 10);...
2
by: Markjan | last post by:
I have a problem with classes and structures in classes (C++) I have to overload operator . class Data { public: class Proxy { //for overload operator Data& _a; int...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.