creating base class (or whatever it is called) ?

C

Craig Joyce

Hi!,

I want to make a class that may do the following:

class T
{
......
}

int main()
{
T k = new T(3, 3);
/* k is now an array of 3, 3 integers */

k[2][1]=4; /* this is what I don't know how to do */
cout<<k[2][1];
return 0;

}

The trouble is that I don't know the keywords to search for to know
more about this. I thought 'base class' may be the way to describe
such a class so I posted it with the subject.
I hope someone helps this moron out,
-Craig
 
V

Victor Bazarov

Craig Joyce said:
I want to make a class that may do the following:

class T
{
......
}

int main()
{
T k = new T(3, 3);
/* k is now an array of 3, 3 integers */

k[2][1]=4; /* this is what I don't know how to do */
cout<<k[2][1];
return 0;

}

The trouble is that I don't know the keywords to search for to know
more about this. I thought 'base class' may be the way to describe
such a class so I posted it with the subject.

What you need is to implement a class that overloads the indexing
operator (operator[]) so that "elements" could be accessed using
it.

class T
{
vector<vector<int> > storage; // where the elements are
public:
T(int w, int h);
vector<int>& operator[](int);
const vector<int>& operator[](int) const;
};

That's a minimalist interface your 'T' should probably have. You
get to fill the gaps.

Now, when you create it, you will have to probably create an object
without using 'new':

T k(3,3);

Victor
 
J

John Harrison

Craig Joyce said:
Hi!,

I want to make a class that may do the following:

class T
{
......
}

int main()
{
T k = new T(3, 3);
/* k is now an array of 3, 3 integers */

k[2][1]=4; /* this is what I don't know how to do */
cout<<k[2][1];
return 0;

}

The trouble is that I don't know the keywords to search for to know
more about this. I thought 'base class' may be the way to describe
such a class so I posted it with the subject.
I hope someone helps this moron out,
-Craig

Nothing to do with base classes, operator overloading and proxy classes are
what you want. In short you will overload the operator[] for T, to return a
proxy class which will also have the operator[] overloaded.

john
 
K

Klaus Eichner

Craig Joyce said:
Hi!,

I want to make a class that may do the following:

class T
{
......
}
// replace "class T {...}" by the following:
#include said:
int main()
{
T k = new T(3, 3);

// replace the above "T k = new T(3, 3);" by the following 4 lines:

std::vector<std::vector<int> > k; // don't forget the extra space between
the two ">" ">"
k.resize(3);
k[0].resize(3);
k[1].resize(3);
k[2].resize(3);
/* k is now an array of 3, 3 integers */

k[2][1]=4; /* this is what I don't know how to do */
cout<<k[2][1];

// the above two lines work fine with std::vector said:
return 0;

}

The trouble is that I don't know the keywords to search for to know
more about this.

Although it is not immediately obvious, the keyword to search for (as far as
standard C++ is concerned) would be 'vector'.
I thought 'base class' may be the way to describe
such a class so I posted it with the subject.

There might exist a way in standard C++ to implement an array of 3, 3
integers using a base class, but this would probably be very complicated
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top