Can we create an object just with constructor name without ()

Hello,

I saw someone uses the constructor like this:

class Foo
{
Foo();
}

int main(){
Foo f= new Foo;
}

what is the difference between Foo f= new Foo; and Foo f= new Foo();

Thanks!
There's none. The language will allow you to use that syntax to call constructors without parameters.
Thanks!

But, which way is better?
Like I said, there's no difference. Both syntaxes compile to identical machine code.
If you're talking about things such as clarity, that's subjective. Some people might get confused if they find both used interchangeably, while others just might not care. There may also be convention concerns to consider if you're part of a team.
Especially when writing generic code using templates the syntax with the parentheses is always preferable.

There can be runtime difference, though.

 
int* pInt = new int;


creates a pointer to an uninitialized integer.

 
int* pInt = new int();


creates a pointer to an integer initialized to zero.
int* pInt = new int;

creates a pointer to an uninitialized integer.

Not according to gcc.
Last edited on
Run this program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string.h>

using namespace std;

int main() {
  char* buf = new char[ 100 ];
  memset( buf, 0xFF, 100 );
  cout << "Address of buf = " << reinterpret_cast<int*>( buf ) << endl;
  delete [] buf;

  int* pi1 = new int;
  int* pi2 = new int();
  cout << "Address of pi1 = " << pi1 << endl;
  cout << "Address of pi2 = " << pi2 << endl;
  cout << *pi1 << ", " << *pi2 << endl;

  for( size_t i = 0; i < 100; ++i )
    cout << (static_cast<unsigned>( buf[ i ] )&0xFF) << " ";

  cout << endl;
}


On my machine it outputs:
Address of buf = 0x9a3c008
Address of pi1 = 0x9a3c008
Address of pi2 = 0x9a3c018
-1, 0
255 255 255 255 255 255 255 255 255 255 255 255 17 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 225 15 2 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255

which clearly shows that
 
new int;


does not touch the memory pointed to by the pointer but

 
new int();


sets the integer to 0.

/home/jsmith> gcc --version
gcc (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Will you look at that...
I stand corrected.
No worries. For the longest time it never occurred to me they were different. I only began paying attention to it when I started doing template programming. For example,

1
2
3
4
5
6
7
template< typename T >
class Foo {
  public:
     Foo() {}
  private:
     T data;
};


default constructs data which, for non-POD types means it runs the type's default constructor, which should initialize data to some reasonable value. But as soon as
Foo is used with a POD-type (such as int), data is undefined because int's default constructor does nothing. Which is annoying if you're providing a template class like this whose default constructor wants to guarantee some kind of initial state to the user.

To fix this, you do:

1
2
3
4
5
6
7
8
9
template< typename T >
class Foo {
  public:
     Foo() : 
          data()     // <---- note the parens!
     {}
  private:
     T data;
};


when T = int, this means that data is assigned the value zero in this context.
(And this works for all other POD types, too -- char, float, double, etc.)

The problem with this syntax is that you can only do it in initializer lists because, for example:

 
  int x();


forward declares a function named x that returns an int and takes no parameters, rather than declaring a variable of type int named x defaulted to 0.

This ambiguity in the grammar is what prompted the value_initialized template class in the boost library (http://www.boost.org).

Topic archived. No new replies allowed.