Instance variable and pointer variable

P

Prasanth

I am novice to c++,
I have the following code :

class A{
int c;
};

What is the difference between the declarations

A a;
A *a = new A();

While creating any object compiler checks for whether there is enough
memory or not. cane someone please elaborate in detail
what happens in the perspective of the memory allocations in both the
cases.... ??? And in what situations we use this.. ??
 
R

Rolf Magnus

Prasanth said:
I am novice to c++,
I have the following code :

class A{
int c;
};

Note that this class's only member is not accessible.
What is the difference between the declarations

A a;
A *a = new A();

While creating any object compiler checks for whether there is enough
memory or not. cane someone please elaborate in detail
what happens in the perspective of the memory allocations in both the
cases.... ??? And in what situations we use this.. ??

Doesn't your C++ book describe this in detail already?
 
P

Paul N

I am novice to c++,
I have the following code :

class A{
             int c;

};

What is the difference between the declarations

A a;
A *a = new A();

While creating any object compiler checks for whether there is enough
memory or not. cane someone please elaborate in detail
what happens in the perspective of the memory allocations in both the
cases.... ??? And in what situations we use this.. ??

Here's an example to illustrate the difference:

A *x; // global variable

void fun() {
A a;
A *b = new A();
x = b;
}

In this case, a is created when the function starts, and is destroyed
again when the function finishes. On the other hand, the one pointed
at by b is created when the function gets to the "new", but continues
to live until it is explicitly destroyed by using "delete". I've added
a global variable just so that there is a way for other parts of the
program can find the created object. Better methods are available.

Hope that helps.
Paul.
 
R

red floyd

I am novice to c++,
I have the following code :

class A{
int c;
};

What is the difference between the declarations

A a;
A *a = new A();

While creating any object compiler checks for whether there is enough
memory or not. cane someone please elaborate in detail
what happens in the perspective of the memory allocations in both the
cases.... ??? And in what situations we use this.. ??

Your answer can be found in FAQ 5.2

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2
 
P

Prasanth

Here's an example to illustrate the difference:

A *x; // global variable

void fun() {
A a;
A *b = new A();
x = b;

}

In this case, a is created when the function starts, and is destroyed
again when the function finishes. On the other hand, the one pointed
at by b is created when the function gets to the "new", but continues
to live until it is explicitly destroyed by using "delete". I've added
a global variable just so that there is a way for other parts of the
program can find the created object. Better methods are available.

Hope that helps.
Paul.

Thanks for the answer...

Thanks,
Prasanth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top