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:)

Re: a simple question{dynamically create object} 80 80

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.

Re: a simple question{dynamically create object} 80 80

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.

Re: a simple question{dynamically create object} 80 80

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

Re: a simple question{dynamically create object} 80 80

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:

Re: a simple question{dynamically create object} 80 80

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 . . .
Re: a simple question{dynamically create object} 80 80

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.

Re: a simple question{dynamically create object} 80 80

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

Re: a simple question{dynamically create object} 80 80

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.

Re: a simple question{dynamically create object} 80 80

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.

Re: a simple question{dynamically create object} 80 80

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

Re: a simple question{dynamically create object} 80 80

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 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.