hi,

sorry if this is a stupid question, i am a total newbie:)

i want to dynamically allocate an object to a pointer and i write:

class_test *ptr = new class_test();
class_test *ptr = new class_test;

both of them compile:confused:....are they the same thing{meaning that the second calls the default constructor}?

which one should i use?

thanks:)

they are the same -- the first form is normally used to pass parameters to the class constructor. The second form when there are no parameters to pass.

Okay to put it in a simple way, the round bracket syntax is used when you want to initialise the primitive data type varibles in your class with their defalut value while you ignore the brackets you just get an uninitialized object.

class My
{ 
   public:
      int a ;
} ;

int main( )
{
    My* first = new My ;
    My* second = new My() ;
   
    // this will print out junk i.e. uninitialized value
    cout << "value of var init without round brackets : " << first->a ;
   
    // this will print out 0 i.e. initialized value
    cout << "value of var init with round brackets : " << second->a ;
}

Similarly the round brackets beome mandatory if you want to pass values to the class constructor -- something you cant do by ignoring the round brackets.

If you want to see a full blown explanation you can look here

Hope it helped, bye.

you example produces the same output when compiled with my compiler.

Hmm maybe it is because you must be using MS Visual studio. BTW I am using MingW GCC latest compiler.

I am attaching the executable, you can see for yourself that the thing works in my case:

Below is the output of the three compilers I use.

This is Dev-C++

value of var init without round brackets : 0
value of var init with round brackets : 0

M$ VC++ 6.0

value of var init without round brackets : -842150451
value of var init with round brackets : -842150451
Press any key to continue

VC++ 2005 Pro

value of var init without round brackets : -842150451
value of var init with round brackets : 0
Press any key to continue . . .

Hmm.. so it figures that the VS 2005 has the same output as the GCC compiler. Strange...

Better stick to the round bracket notation since it always seems to initialize the POD.

thanks for your answers....i also have visual studio....so i can't check the difference....

we might be seeing undefined behavior in those examples. I made the program a little bit more complex by adding a base class and VC++ 2005 produced different results than before

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;
class base
{
public:
    base() {m_xx = 1;}
    int getXX() {return m_xx;}
private:
    int m_xx;
};

class My : public base
{ 

   public:
      int a ;
} ;
 
int main( )
{
    My* first = new My ;
    My* second = new My() ;
   
    // this will print out junk i.e. uninitialized value
    cout << "value of var init without round brackets : " << first->a << " " << first->getXX() << "\n";
   
    // this will print out 0 i.e. initialized value
    cout << "value of var init with round brackets : " << second->a << " " << second->getXX() << "\n" ;
}

results

value of var init without round brackets : -842150451 1
value of var init with round brackets : -842150451 1
Press any key to continue . . .

Don't count on () version initializing POD data because it might not. If you need them to be initialized then write a constructor that will explicitly do it.

When the class "My" inherited from class "Base" it no longer remained a POD hence the different results. YOu can be rest assured that the ( ) notation always initializes the POD data.

Since inheritance is not supported in C, the moment My inherited from Base, it voilated the rule, since there is no equivalent of inheritance in C i.e. no conversion possible for the class My to make it C compatible.

Conclusion:

You can trust on ( ) to initialize "ONLY" POD and that too if the pointer is of type POD.

>>Since inheritance is not supported in C
neither is the new operator and classes.:rolleyes:

neither is the new operator and classes.

Naa you are missing the whole point. POD rule is voilated is any of those things are used which cant be represented or have equivalent in C.

Equivalent of new in C => malloc and calloc
Equivalent of classes in C => structs

I hope you get the point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.