|
-
September 8th, 2010, 05:44 AM
#1
Difference between object creation on stack and on heap
Hi All,
Is there any difference in the way object is created on stack and on heap?
I have come across a strange problem. Code snippet is as follows:
class A
{
private:
int a;
float b;
char c;
void Print()
{
cout<<"a = "<<a<<" b = "<<b<<" c = "<<c<<endl;
}
};
When I use
A *a = new A;
a->Print();
it compiles and prints some garbage values(as expected), which is fine.
But when I use
A a;
a.Print();
it gives compilation errors 1. 'a.A::a' is used uninitialized in this function 2. 'a.A::b' is used uninitialized in this function 3. 'a.A::c' is used uninitialized in this function
Can anyone please explain why is this happening??
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|