Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > creating base class (or whatever it is called) ?

Reply
Thread Tools

creating base class (or whatever it is called) ?

 
 
Craig Joyce
Guest
Posts: n/a
 
      06-23-2003
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-23-2003
"Craig Joyce" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      06-23-2003

"Craig Joyce" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
Klaus Eichner
Guest
Posts: n/a
 
      06-23-2003
"Craig Joyce" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Base class method Need base class value Karan Rajput Ruby 2 12-22-2010 04:47 PM
base class public type (non template and template base class) Hicham Mouline C++ 1 04-20-2009 03:28 PM
Access of base class' private base class: qualification required, why Alf P. Steinbach C++ 6 09-03-2005 04:03 PM
Creating inherited class with set base class Josh Mcfarlane C++ 2 08-18-2005 05:31 PM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 2 12-01-2003 06:31 AM



Advertisments