944,015 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12303
  • C++ RSS
Mar 20th, 2006
0

Class with dynamic array how?

Expand Post »
I am new in programming and my problem is probably notorious but still is a problem.
I want build class which can produce object which contain two dimensional dynamic array and member function which fill that array.
Question is where I should to create the array, in function or in constructor.
Cod of array is:

int sizeP, stupaca;
char **polje; //dinamicka alokacija polja s pozivacem na pokazivac
polje = new char*[sizeP];
for (int i = 0; i < sizeP; i++)
{
polje[i] = new char[stupaca + 1];
polje[i][0] = stupaca;
for (int j = 1; j <= stupaca; j++)
polje[i][j] = i + 1 + j / 100.;
}


2. question: do I need copy constructor? Probably yes, but I don’t know create it.
3.Can I delete the array in destructor with following cod:

for (int i = 0; i < sizeP; i++)
delete [] polje[i];
delete [] polje;




Thenx
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marioxp is offline Offline
5 posts
since Mar 2006
Mar 21st, 2006
0

Re: Class with dynamic array how?

Well, when someone says dynamic array my knee jerk reaction would be use <vector> from the STL. However, I'm not too sure about 2d vectors although I guess its inherent structure would probably be the same.

If you're intent is to build this from scratch for fun then go for it, although you probably haven't found my reply useful.

hmmm
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 21st, 2006
0

Re: Class with dynamic array how?

i hope this can help u a little....
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. using namespace std ;
  3.  
  4. class DynamecArray2D ;
  5. class DynamecArray2D {
  6. char **pArray ;
  7. public:
  8. DynamecArray2D() ;
  9. DynamecArray2D(int iRow, int iColumn) ;
  10. DynamecArray2D(DynamecArray2D &) ; //copy constructor
  11. ~DynamecArray2D() ;
  12. };
  13. DynamecArray2D::DynamecArray2D() {
  14. pArray = NULL;
  15. }
  16. DynamecArray2D::DynamecArray2D(int iRow, int iColumn) {
  17. pArray = new char*[iRow] ;
  18. for(int i=0; i<iRow; ++i) {
  19. pArray[i] = new char[iColumn] ;
  20. pArray[i][iColumn-1] = NULL ;
  21. }
  22. }
  23. DynamecArray2D::DynamecArray2D(DynamecArray2D &objDynamecArray2D) { //copy constructor
  24. char **p=NULL;
  25. int i=0,j=0,iColumn;
  26. p=objDynamecArray2D.pArray ;
  27. while(*(p+i)) {
  28. ++i;
  29. }
  30. pArray = new char*[i] ;
  31. iColumn = strlen(objDynamecArray2D.pArray[0]) ;
  32. for(j=0; j<i; ++j) {
  33. pArray[j] = new char[iColumn+1] ;
  34. pArray[j][iColumn-1] = NULL ;
  35. }
  36. i=0;
  37. p=objDynamecArray2D.pArray ;
  38. while(*(p+i)) {
  39. strcpy(*(pArray+i),*(p+i));
  40. ++i;
  41. }
  42. }
  43. DynamecArray2D::~DynamecArray2D() {
  44. int i=0;
  45. while(pArray[i]) {
  46. if(pArray[i]) {
  47. delete pArray[i] ;
  48. }
  49. ++i;
  50. }
  51. if(pArray){
  52. delete []pArray ;
  53. }
  54. }
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Mar 21st, 2006
0

Re: Class with dynamic array how?

>I'm not too sure about 2d vectors although I guess its inherent structure would probably be the same.
C++ Syntax (Toggle Plain Text)
  1. std::vector<std::vector<T> > matrix;
std::vector is designed to work the way people expect even in multiple dimensions.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 21st, 2006
0

Re: Class with dynamic array how?

Thank you I’ll try with that. I think this is it!

I don't use this for vectors I’m trying create from one big string many small strings, and place it in array.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marioxp is offline Offline
5 posts
since Mar 2006
Mar 21st, 2006
0

Re: Class with dynamic array how?

>std::vector<std::vector<T> > matrix;

Thanx _jsw, I was wondering that but I didn't know 4 sure. Do you have an example...

>I don't use this for vectors I’m trying create from one big string many small strings, and place it in array.

Well unless, you just missed the above, you can use a 2D vector to do exactly that. Unless, like I said before ,you're just doing that for fun, practise or just to complicate matters?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 22nd, 2006
0

Re: Class with dynamic array how?

>Do you have an example...
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. int main()
  5. {
  6. typedef std::vector<std::vector<int> > mat_t;
  7.  
  8. mat_t mat ( 10, std::vector<int> ( 10, 0 ) );
  9.  
  10. for ( mat_t::size_type i = 0; i < mat.size(); i++ ) {
  11. for ( mat_t::size_type j = 0; j < mat[i].size(); j++ ) {
  12. if ( j % 2 != 0 )
  13. mat[i][j] = j;
  14. }
  15. }
  16.  
  17. for ( mat_t::size_type i = 0; i < mat.size(); i++ ) {
  18. for ( mat_t::size_type j = 0; j < mat[i].size(); j++ )
  19. std::cout<< mat[i][j] <<' ';
  20. std::cout<<'\n';
  21. }
  22. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++
Next Thread in C++ Forum Timeline: <string>





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC