creating an array of objects dynamically
-
i have defined two class that you see below:
class Row
{
private:
char *termState;
int *terms;
public:
Row(int length,int mTCount);};
class Table
{
private:
Row *rows;
public:
Table(int);
}in Table() constructor i need to creat an array of Row objects dynamically like this :
Table::Table(int n)
{
rows = new Row[n];
}how i can call the Row() constructor for this array? is this possible? more information : in Row() constructor i want to allocate memory for (char *termState) and (int *terms) dynamically;
just a guess, why don't you use
std::vector<>
?
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
just a guess, why don't you use
std::vector<>
?
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
std::vector<CMyType> vec;
CMyType o1, o2;
vec.push_back(o1);
vec.push_back(o2);//to iterate through the vector :
std::vector<CMyType>::iterator iter;
for (iter = vec.begin(); iter != vec.end(); iter++) {
CMyType obj = *iter;
//...
}
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
std::vector<CMyType> vec;
CMyType o1, o2;
vec.push_back(o1);
vec.push_back(o2);//to iterate through the vector :
std::vector<CMyType>::iterator iter;
for (iter = vec.begin(); iter != vec.end(); iter++) {
CMyType obj = *iter;
//...
}
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
i have defined two class that you see below:
class Row
{
private:
char *termState;
int *terms;
public:
Row(int length,int mTCount);};
class Table
{
private:
Row *rows;
public:
Table(int);
}in Table() constructor i need to creat an array of Row objects dynamically like this :
Table::Table(int n)
{
rows = new Row[n];
}how i can call the Row() constructor for this array? is this possible? more information : in Row() constructor i want to allocate memory for (char *termState) and (int *terms) dynamically;
erfi wrote:
how i can call the Row() constructor for this array? is this possible?
It is possible to call the constructor of the row class. It get's invoked automatically on creation of an object. ex: This code is not optimized. You should be using destructor of the classes to free the total amount of memory being allocated in this process.
#include "stdafx.h" #include using namespace std; class Row { private: char *termState; int *terms; public: Row(int length,int mTCount); void Display(); }; Row::Row(int a,int b) { cout<<"Constructor of the row class is invoked"<Display(); } } int _tmain(int argc, _TCHAR* argv[]) { Table t(10); return 0; }
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
erfi wrote:
how i can call the Row() constructor for this array? is this possible?
It is possible to call the constructor of the row class. It get's invoked automatically on creation of an object. ex: This code is not optimized. You should be using destructor of the classes to free the total amount of memory being allocated in this process.
#include "stdafx.h" #include using namespace std; class Row { private: char *termState; int *terms; public: Row(int length,int mTCount); void Display(); }; Row::Row(int a,int b) { cout<<"Constructor of the row class is invoked"<Display(); } } int _tmain(int argc, _TCHAR* argv[]) { Table t(10); return 0; }
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
thank you _AnShUmAn_ but in your code you write this :
Row *rows[5];
but i need a parametric length for rows, like this:
Table::Table(int a)
{
rows = new Row[a];
}when i compile this code this error appears : "error C2512: 'Row' : no appropriate default constructor available" i think that there is a better way: i want to delete the Row() constructor and after creating the Row objects dynamically , allocate the memory for Row object's member with a member function of Row. what do you think ? is this a good way?
-
thank you _AnShUmAn_ but in your code you write this :
Row *rows[5];
but i need a parametric length for rows, like this:
Table::Table(int a)
{
rows = new Row[a];
}when i compile this code this error appears : "error C2512: 'Row' : no appropriate default constructor available" i think that there is a better way: i want to delete the Row() constructor and after creating the Row objects dynamically , allocate the memory for Row object's member with a member function of Row. what do you think ? is this a good way?
I agree with toxcc - using vectors for stuff like this would be much easier, as they're especially designed to work just like arrays, but to be resizable at runtime
-- Help me! I'm turning into a grapefruit! Buzzwords!
-
thank you _AnShUmAn_ but in your code you write this :
Row *rows[5];
but i need a parametric length for rows, like this:
Table::Table(int a)
{
rows = new Row[a];
}when i compile this code this error appears : "error C2512: 'Row' : no appropriate default constructor available" i think that there is a better way: i want to delete the Row() constructor and after creating the Row objects dynamically , allocate the memory for Row object's member with a member function of Row. what do you think ? is this a good way?
erfi wrote:
"error C2512: 'Row' : no appropriate default constructor available"
try providing a default constructor in your class.
erfi wrote:
allocate the memory for Row object's member with a member function of Row.
What's the problem with allocation of memory in the constructor itself. Here's the code for you:
#include "stdafx.h" #include using namespace std; class Row { private: char *termState; int *terms; public: Row() { cout<<"In edefau"<Display(); } int _tmain(int argc, _TCHAR* argv[]) { Table t(10); return 0; }
Try this out and let me know whether there exist some more problem.. -- modified at 9:42 Wednesday 20th September, 2006
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
just a guess, why don't you use
std::vector<>
?
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
Alternatively, he could use the boost/tr1
array
template as well.If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
erfi wrote:
"error C2512: 'Row' : no appropriate default constructor available"
try providing a default constructor in your class.
erfi wrote:
allocate the memory for Row object's member with a member function of Row.
What's the problem with allocation of memory in the constructor itself. Here's the code for you:
#include "stdafx.h" #include using namespace std; class Row { private: char *termState; int *terms; public: Row() { cout<<"In edefau"<Display(); } int _tmain(int argc, _TCHAR* argv[]) { Table t(10); return 0; }
Try this out and let me know whether there exist some more problem.. -- modified at 9:42 Wednesday 20th September, 2006
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
dude, stop trying to reinvent the wheel. the STL provides all this already, in a better way... consider using vector ;P
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]