| | |
Class with dynamic array how?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 5
Reputation:
Solved Threads: 0
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:
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:
Thenx
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
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
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
*Voted best profile in the world*
i hope this can help u a little....
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std ; class DynamecArray2D ; class DynamecArray2D { char **pArray ; public: DynamecArray2D() ; DynamecArray2D(int iRow, int iColumn) ; DynamecArray2D(DynamecArray2D &) ; //copy constructor ~DynamecArray2D() ; }; DynamecArray2D::DynamecArray2D() { pArray = NULL; } DynamecArray2D::DynamecArray2D(int iRow, int iColumn) { pArray = new char*[iRow] ; for(int i=0; i<iRow; ++i) { pArray[i] = new char[iColumn] ; pArray[i][iColumn-1] = NULL ; } } DynamecArray2D::DynamecArray2D(DynamecArray2D &objDynamecArray2D) { //copy constructor char **p=NULL; int i=0,j=0,iColumn; p=objDynamecArray2D.pArray ; while(*(p+i)) { ++i; } pArray = new char*[i] ; iColumn = strlen(objDynamecArray2D.pArray[0]) ; for(j=0; j<i; ++j) { pArray[j] = new char[iColumn+1] ; pArray[j][iColumn-1] = NULL ; } i=0; p=objDynamecArray2D.pArray ; while(*(p+i)) { strcpy(*(pArray+i),*(p+i)); ++i; } } DynamecArray2D::~DynamecArray2D() { int i=0; while(pArray[i]) { if(pArray[i]) { delete pArray[i] ; } ++i; } if(pArray){ delete []pArray ; } }
>I'm not too sure about 2d vectors although I guess its inherent structure would probably be the same.
std::vector is designed to work the way people expect even in multiple dimensions.
C++ Syntax (Toggle Plain Text)
std::vector<std::vector<T> > matrix;
New members chased away this month: 5
>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?
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?
*Voted best profile in the world*
>Do you have an example...
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> int main() { typedef std::vector<std::vector<int> > mat_t; mat_t mat ( 10, std::vector<int> ( 10, 0 ) ); for ( mat_t::size_type i = 0; i < mat.size(); i++ ) { for ( mat_t::size_type j = 0; j < mat[i].size(); j++ ) { if ( j % 2 != 0 ) mat[i][j] = j; } } for ( mat_t::size_type i = 0; i < mat.size(); i++ ) { for ( mat_t::size_type j = 0; j < mat[i].size(); j++ ) std::cout<< mat[i][j] <<' '; std::cout<<'\n'; } }
New members chased away this month: 5
![]() |
Views: 6983 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






