Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Why use new?
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 23 Topic 44133 of 47561
Post > Topic >>

Why use new?

by saneman <asdfsdf@[EMAIL PROTECTED] > Mar 26, 2008 at 07:04 PM

In the below code I make 2 instances of the class Bob. The first 
instance does not use new (automatic allocation?) and the second 
instance is used with new (dynamic allocation?)


#include<iostream>

class Bob {
	public:
		Bob(int a): n(a) {}
		
		int getn() {
			return n;
		}
		
	private:
		int n;

};

int main() {
	Bob b1(22);
	printf("n = %d\n", b1.getn());
	
	Bob* b2 = new Bob(88);
	printf("n = %d\n", b2->getn());
	
	return 0;
}


But when are there any reason to use new?

And will the b2 pointer automatically be deallocated when main returns?
 




 23 Posts in Topic:
Why use new?
saneman <asdfsdf@[EMAI  2008-03-26 19:04:24 
Re: Why use new?
"Victor Bazarov"  2008-03-26 14:35:32 
Re: Why use new?
saneman <asdfsdf@[EMAI  2008-03-26 20:32:51 
Re: Why use new?
"Victor Bazarov"  2008-03-26 15:41:53 
Re: Why use new?
James Kanze <james.kan  2008-03-26 15:12:39 
Re: Why use new?
sean_in_raleigh@[EMAIL PR  2008-03-26 11:36:13 
Re: Why use new?
"jason.cipriani@[EMA  2008-03-26 12:33:03 
Re: Why use new?
James Kanze <james.kan  2008-03-26 15:10:05 
Re: Why use new?
Paul Brettschneider <p  2008-03-26 23:43:30 
Re: Why use new?
peter koch <peter.koch  2008-03-26 17:38:33 
Re: Why use new?
"Fred Zwarts" &  2008-03-27 09:47:21 
Re: Why use new?
James Kanze <james.kan  2008-03-27 02:45:58 
Re: Why use new?
Paul Brettschneider <p  2008-03-27 20:37:12 
Re: Why use new?
Ian Collins <ian-news@  2008-03-28 09:10:15 
Re: Why use new?
Paul Brettschneider <p  2008-03-27 21:59:20 
Re: Why use new?
Ian Collins <ian-news@  2008-03-28 10:15:11 
Re: Why use new?
James Kanze <james.kan  2008-03-27 02:50:30 
Re: Why use new?
James Kanze <james.kan  2008-03-28 01:31:59 
Re: Why use new?
Paul Brettschneider <p  2008-03-28 21:39:23 
Re: Why use new?
James Kanze <james.kan  2008-03-28 01:44:16 
Re: Why use new?
Ian Collins <ian-news@  2008-03-28 22:48:35 
Re: Why use new?
James Kanze <james.kan  2008-03-28 15:27:00 
Re: Why use new?
Andrey Tarasevich <and  2008-03-27 00:05:14 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Sep 7 3:38:18 CDT 2008.