Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 16th, 2005, 12:05 PM
Tony Johansson
Guest
 
Posts: n/a
Default A question about inheritance with template

Hello experts!

I have two class template below with names Array and CheckedArray.
The class template CheckedArray is derived from the class template Array
which is the base class

This program works fine but there in one thing that I'm unsure about
and that is the inheritance statement.
What difference is it if I have this construction
class CheckedArray : public Array<T>
compared to this construction. .
class CheckedArray : public Array
Note the type parameter <T> has been removed

I don't get any compile error or run time error if I use the
last statement that with the missing <T> for the base class
See definition for the class template CheckedArray
about this class CheckedArray : public Array<T>


From here to the end is code for the two class templates Array and
CheckedArray
**************************************
Here start the definition of the class template Array
***************************************
template <typename T>
class Array
{
public:
Array(int s=100) : size(s)
{ array = new T[size]; }

Array(const Array<T>& ar) : size(ar.size)
{
array = new T[ar.size];
for(int i=0; i<size; i++)
array[i] = ar.array[i];
}

bool operator==(const Array<T>& ar)
{
for (int i=0; i<size; i++)
if (array[i] != ar.array[i])
return false;
return true;
}

Array<T>& operator=(const Array<T>& ar)
{
if (this == &ar)
return *this;
if (size != ar.size)
{
size = ar.size;
delete[] array;
array = new T[size];
for(int i=0; i<size; i++)
array[i] = ar.array[i];
return *this;
}
}

virtual ~Array()
{ delete[] array; }

virtual const T& operator[](int i) const
{ return array[i]; }

virtual T& operator[](int i)
{ return array[i]; }

int getSize() const
{ return size; }

typedef T element_type;

protected:
int size;
T* array;
};
************************************************** *****************
Here start the definition of the class template CheckedArray
************************************************** ******************
template <typename T>
//class CheckedArray : public Array // Alternative 1
class CheckedArray : public Array<T> // Alternative 2

{
public:
CheckedArray(int = 100);
CheckedArray(const CheckedArray<T>&);
CheckedArray<T>& operator=(const CheckedArray<T>&);
virtual ~CheckedArray();
virtual const T& operator[](int) const;
virtual T& operator[](int);
bool operator==(const CheckedArray<T>&);
};

template <typename T>
bool operator==(const CheckedArray<T>&, const CheckedArray<T>&);

template <typename T>
bool operator!=(const CheckedArray<T>&, const CheckedArray<T>&);

template <typename T>
CheckedArray<T>::CheckedArray(int size) : Array<T>(size) {}

template <typename T>
CheckedArray<T>::CheckedArray(const CheckedArray<T>& ar) : Array<T>(ar) {}

template <typename T>
CheckedArray<T>& CheckedArray<T>::operator=(const CheckedArray<T>& ar)
{
if (this == &ar)
return *this;
if (size != ar.size() ) cout << "Error" << endl;
Array<T>::operator=(ar);
return *this;
}

template <typename T>
bool CheckedArray<T>::operator==(const CheckedArray<T>& ar)
{
if (getSize() != ar.getSize() ) cout << "error" << endl;
return Array<T>::operator==(ar);
}

template <typename T>
CheckedArray<T>::~CheckedArray() {}

template <typename T>
T& CheckedArray<T>::operator[](int i)
{
if (i < 0 || i >= size) cout << "error" << endl;
return array[i];
}

template <typename T>
const T& CheckedArray<T>::operator[](int i) const
{
if (i < 0 || i >= size) cout << "error" << endl;
return array[i];
}

template <typename T>
bool operator!=(const CheckedArray<T>& ar1, const CheckedArray<T>& ar2)
{ return !(ar1 == ar2); }

Many thanks

//Tony


  #2  
Old August 16th, 2005, 12:15 PM
Maxim Yegorushkin
Guest
 
Posts: n/a
Default Re: A question about inheritance with template

Tony Johansson wrote:[color=blue]
> Hello experts!
>
> I have two class template below with names Array and CheckedArray.
> The class template CheckedArray is derived from the class template Array
> which is the base class
>
> This program works fine but there in one thing that I'm unsure about
> and that is the inheritance statement.
> What difference is it if I have this construction
> class CheckedArray : public Array<T>
> compared to this construction. .
> class CheckedArray : public Array
> Note the type parameter <T> has been removed
>
> I don't get any compile error or run time error if I use the
> last statement that with the missing <T> for the base class
> See definition for the class template CheckedArray
> about this class CheckedArray : public Array<T>[/color]

You don't get any error because you don't instantiate CheckedArray. A
conforming compiler probably should issue an error, because in the
later case Array is not argument dependent and should be looked up at
the point of CheckedArray's declaration. But some compilers defer the
lookup till instantiation time.

Try instatiating a CheckedArray by adding a line with:

template class CheckedArray<int>;

  #3  
Old August 16th, 2005, 12:25 PM
Tony Johansson
Guest
 
Posts: n/a
Default Re: A question about inheritance with template


"Maxim Yegorushkin" <maxim.yegorushkin@gmail.com> skrev i meddelandet
news:1124190418.860042.261590@g47g2000cwa.googlegr oups.com...[color=blue]
> Tony Johansson wrote:[color=green]
>> Hello experts!
>>
>> I have two class template below with names Array and CheckedArray.
>> The class template CheckedArray is derived from the class template Array
>> which is the base class
>>
>> This program works fine but there in one thing that I'm unsure about
>> and that is the inheritance statement.
>> What difference is it if I have this construction
>> class CheckedArray : public Array<T>
>> compared to this construction. .
>> class CheckedArray : public Array
>> Note the type parameter <T> has been removed
>>
>> I don't get any compile error or run time error if I use the
>> last statement that with the missing <T> for the base class
>> See definition for the class template CheckedArray
>> about this class CheckedArray : public Array<T>[/color]
>
> You don't get any error because you don't instantiate CheckedArray. A
> conforming compiler probably should issue an error, because in the
> later case Array is not argument dependent and should be looked up at
> the point of CheckedArray's declaration. But some compilers defer the
> lookup till instantiation time.
>
> Try instatiating a CheckedArray by adding a line with:
>
> template class CheckedArray<int>;[/color]

What do you mean with this template class CheckedArray<int>; ?
Explain that to me?
Is it to instansiate an object of class template CheckedArray
like CheckedArray<int> c(1);

When I do add this statement CheckedArray<int> c(1);
to the main program I get compile error if I use CheckedArray : public Array
and that is what you pointed out to me. I had forgot to do so in the main
program

Many thanks

//Tony




  #4  
Old August 16th, 2005, 01:05 PM
benben
Guest
 
Posts: n/a
Default Re: A question about inheritance with template

Ok, if you instantiate a type, you get an object; if you instantiate a class
template, you get a type (class type).

Given

template <typename T>
class stack{ /* ... */ };

stack<int> buff;
stack<string> contacts;
stack<stack<bool> > freak;

then
* stack is NOT a type, its a class template;
* stack<int>, stack<string>, stack<stack<bool> > are types, used just as
int and char;
* buff, contacts and freak are objects (variables).

Now, because stack<int>, stack<string> and stack<stack<bool> > are class
types, you can inherit from them:

class contact_list: public stack<string>
{ /* ... */ };

The derive class can itself be an instantiation of a class template:

template <typename T>
class improved_stack: private stack<T>
{ /* ... */ };

Therefore, improved_stack<int> derives from stack<int>,
improved_stack<string> derives from stack<string>...

However, you can't inherit from a non-type, such as a class template:

class silly_stack:
private stack //error, stack of what?
{ /* ... */ };

Regards,
Ben

P.S. I would like to kindly recommand you to read some book on templates,
C++ Templates, The Complete Guide by Vandevoorde and Josuttis is joy to
read!


  #5  
Old August 16th, 2005, 01:15 PM
Maxim Yegorushkin
Guest
 
Posts: n/a
Default Re: A question about inheritance with template

Tony Johansson wrote:

[]
[color=blue][color=green]
> > Try instatiating a CheckedArray by adding a line with:
> >
> > template class CheckedArray<int>;[/color]
>
> What do you mean with this template class CheckedArray<int>; ?
> Explain that to me?[/color]

This syntax is explicit template instantiation. You make a compiler
instantiate a template and all its non template member functions.
[color=blue]
> Is it to instansiate an object of class template CheckedArray
> like CheckedArray<int> c(1);[/color]

No. Explicit instantiation does not yield an object.

  #6  
Old August 16th, 2005, 02:15 PM
Tony Johansson
Guest
 
Posts: n/a
Default Re: A question about inheritance with template

Good explaind

Thanks

//Tony


"benben" <moc.liamtoh@hgnohneb read backward> skrev i meddelandet
news:4301d37a$0$21235$afc38c87@news.optusnet.com.a u...[color=blue]
> Ok, if you instantiate a type, you get an object; if you instantiate a
> class template, you get a type (class type).
>
> Given
>
> template <typename T>
> class stack{ /* ... */ };
>
> stack<int> buff;
> stack<string> contacts;
> stack<stack<bool> > freak;
>
> then
> * stack is NOT a type, its a class template;
> * stack<int>, stack<string>, stack<stack<bool> > are types, used just
> as int and char;
> * buff, contacts and freak are objects (variables).
>
> Now, because stack<int>, stack<string> and stack<stack<bool> > are class
> types, you can inherit from them:
>
> class contact_list: public stack<string>
> { /* ... */ };
>
> The derive class can itself be an instantiation of a class template:
>
> template <typename T>
> class improved_stack: private stack<T>
> { /* ... */ };
>
> Therefore, improved_stack<int> derives from stack<int>,
> improved_stack<string> derives from stack<string>...
>
> However, you can't inherit from a non-type, such as a class template:
>
> class silly_stack:
> private stack //error, stack of what?
> { /* ... */ };
>
> Regards,
> Ben
>
> P.S. I would like to kindly recommand you to read some book on templates,
> C++ Templates, The Complete Guide by Vandevoorde and Josuttis is joy to
> read!
>[/color]


 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles