Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

class instantiation


Hi,

Assume we have a class called A. Assume it has a constructor which
takes an integer as its  input. What is the difference between these
two instantiations?

A a(1);
and
A a=new A(1);

Thanks.
Sara

sara wrote:
> Assume we have a class called A. Assume it has a constructor which
> takes an integer as its  input. What is the difference between these
> two instantiations?

> A a(1);
> and
> A a=new A(1);

The difference is the first is allowed, and the second will give you
syntax error, because your object should be a pointer to initialize it
like that.
On 4 Juni, 11:51, sara <sarasar@gmail.com> wrote:

> Hi,

> Assume we have a class called A. Assume it has a constructor which
> takes an integer as its  input. What is the difference between these
> two instantiations?

> A a(1);
> and
> A a=new A(1);

Consider the following code:

void test()
{
  A foo(1);
  A* bar = new A(1);

}

In this case both will create a new instance of an object of type A,
but foo will be destroyed as soon as test() returns, because it's
lifetime is limited by the scope in which it was created. bar on the
other hand will continue to exist until you use 'delete' to destroy
it, its lifetime is not limited.

In general you should use the first type when you can and the second
when you must.

--
Erik Wikstrm

In the first step, you are creating a local object whereas in the
second step (assuming you have missded A*) you are creating object
pointer.

The local object will be destroyed as soon as you get out of function
where A a(1) is defined whereas the pointer will linger until you call
delete A.

Hope this makes it clear.

Vaibhav <vaibhav.pa@gmail.com> wrote in message...
> In the first step, you are creating a local object whereas in the
> second step (assuming you have missded A*) you are creating object
> pointer.
> The local object will be destroyed as soon as you get out of function
> where A a(1) is defined whereas the pointer will linger until you call
> delete A.
> Hope this makes it clear.

Makes WHAT clear?

Please read this before your next post:
http://www.parashift.com/c++-faq-lite/how-to-post.html

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc