cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
Simple pointers (dynamic memory allocati
Simple pointers (dynamic memory allocation)
Nov 26, 2010 at 12:49am UTC
mozly
(35)
Im having difficulty with a program that needs to create a certain number of objects depending on the value of variable.
Im working in c++
I want to do this
digit is a class
item is an int with value 6
here is a snippet:
1
2
digit *token; token =
new
digit[item];
this is bad code how can I create 6 digit objects given that the value of item is 6? Or is this impossible?
Nov 26, 2010 at 2:50am UTC
Zhuge
(4664)
The snippet looks fine to me. Just remember to delete[] when you are done. Perhaps you need to be more specific?
Nov 26, 2010 at 3:22am UTC
mozly
(35)
the problem is that the constructor is overloaded and takes either int or string
so it gives me this error:
no appropriate default constructor available
Nov 26, 2010 at 3:32am UTC
firedraco
(6243)
You can't use
new
[]
if you aren't able to use the default constructor.
Nov 26, 2010 at 3:50am UTC
rocketboy9000
(562)
Blargh, screw new [], use malloc, a loop, and a init function if you need pointers to dynamic arrays of classes that need initializers.
Or, just use the std::vector template which will save you from a lot of other trouble.
Topic archived. No new replies allowed.