creating base class (or whatever it is called) ?

Discussion in 'C++' started by Craig Joyce, Jun 23, 2003.

  1. Craig Joyce

    Craig Joyce Guest

    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
    Craig Joyce, Jun 23, 2003
    #1
    1. Advertising

  2. "Craig Joyce" <> wrote...
    > 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
    Victor Bazarov, Jun 23, 2003
    #2
    1. Advertising

  3. "Craig Joyce" <> wrote in message
    news:...
    > 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
    John Harrison, Jun 23, 2003
    #3
  4. "Craig Joyce" <> wrote in message
    news:...
    > Hi!,
    >
    > I want to make a class that may do the following:
    >
    > class T
    > {
    > ......
    > }

    // replace "class T {...}" by the following:
    #include<vector>

    >
    > 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<std::vector<int> >

    > 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
    compared to the std::vector<std::vector<int> > solution.

    > I hope someone helps this moron out,
    > -Craig
    Klaus Eichner, Jun 23, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. z. f.
    Replies:
    0
    Views:
    551
    z. f.
    Oct 19, 2004
  2. Alf P. Steinbach
    Replies:
    6
    Views:
    534
    John Carson
    Sep 3, 2005
  3. Nagrik
    Replies:
    3
    Views:
    361
    Tomás Ó hÉilidhe
    Nov 28, 2007
  4. Hicham Mouline
    Replies:
    1
    Views:
    584
    Victor Bazarov
    Apr 20, 2009
  5. Karan Rajput
    Replies:
    2
    Views:
    131
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page