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==(cons t 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(in t = 100);
CheckedArray(co nst CheckedArray<T> &);
CheckedArray<T> & operator=(const CheckedArray<T> &);
virtual ~CheckedArray() ;
virtual const T& operator[](int) const;
virtual T& operator[](int);
bool operator==(cons t CheckedArray<T> &);
};
template <typename T>
bool operator==(cons t CheckedArray<T> &, const CheckedArray<T> &);
template <typename T>
bool operator!=(cons t 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=(con st CheckedArray<T> & ar)
{
if (this == &ar)
return *this;
if (size != ar.size() ) cout << "Error" << endl;
Array<T>::opera tor=(ar);
return *this;
}
template <typename T>
bool CheckedArray<T> ::operator==(co nst CheckedArray<T> & ar)
{
if (getSize() != ar.getSize() ) cout << "error" << endl;
return Array<T>::opera tor==(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!=(cons t CheckedArray<T> & ar1, const CheckedArray<T> & ar2)
{ return !(ar1 == ar2); }
Many thanks
//Tony 5 2083
Tony Johansson wrote: 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>
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<in t>;
"Maxim Yegorushkin" <ma************ ***@gmail.com> skrev i meddelandet
news:11******** **************@ g47g2000cwa.goo glegroups.com.. . Tony Johansson wrote: 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>
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<in t>;
What do you mean with this template class CheckedArray<in t>; ?
Explain that to me?
Is it to instansiate an object of class template CheckedArray
like CheckedArray<in t> c(1);
When I do add this statement CheckedArray<in t> 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
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<boo l> > freak;
then
* stack is NOT a type, its a class template;
* stack<int>, stack<string>, stack<stack<boo l> > are types, used just as
int and char;
* buff, contacts and freak are objects (variables).
Now, because stack<int>, stack<string> and stack<stack<boo l> > 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!
Tony Johansson wrote:
[] Try instatiating a CheckedArray by adding a line with:
template class CheckedArray<in t>; What do you mean with this template class CheckedArray<in t>; ? Explain that to me?
This syntax is explicit template instantiation. You make a compiler
instantiate a template and all its non template member functions.
Is it to instansiate an object of class template CheckedArray like CheckedArray<in t> c(1);
No. Explicit instantiation does not yield an object.
Good explaind
Thanks
//Tony
"benben" <moc.liamtoh@hg nohneb read backward> skrev i meddelandet
news:43******** *************** @news.optusnet. com.au... 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<boo l> > freak;
then * stack is NOT a type, its a class template; * stack<int>, stack<string>, stack<stack<boo l> > are types, used just as int and char; * buff, contacts and freak are objects (variables).
Now, because stack<int>, stack<string> and stack<stack<boo l> > 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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: jesse |
last post by:
I am frustrated by class specialization. i don't
think it helps me a lot.
suppose we have
template <class T>
class Talkative
{
T& t;
public:
|
by: Gandu |
last post by:
Could some C++ guru please help me? I have a very odd problem with respect
templates and inheritance. I have templatized List class, from which I am
inheriting to create a Stack class. All works fine till I have to create a
Stack object - all sorts of error messages are generated. I have included
source files below:
The List class:
#include "List.h"
template <class T>
|
by: Thomas Matthews |
last post by:
Hi,
I would like to apply inheritance to a template parameter, but my design
fails to compile: cannot initialize one template class with child
child parameterized class. I'll explain...
Given:
#include <string>
// Parent class
|
by: darkstorm |
last post by:
I have a doubt regarding inheritance involving templates
Consider this:
/////////////////////////////////////
template<typename T>
class A
{
private:
T m_a;
|
by: Tony Johansson |
last post by:
Hello Experts!
I reading a book called programming with design pattern revealed
by Tomasz Muldner and here I read something that I don't understand
completely.
Im I right if I say the following.
In this case is the derived class a class template and the base class a
class template.
Both the base class and the derived class will be instansiated with some
| |
by: makc.the.great |
last post by:
now that I am looking at templates, there's the question.
why same effect couldn't/shouldn't be achieved with inheritance?
provided the difference of the two, when and where do I use templates
instead of inheritance, and other way around?
thoughts, please.
|
by: Steve Jorgensen |
last post by:
A while back, I started boning up on Software Engineering best practices and
learning about Agile programming. In the process, I've become much more
committed to removing duplication in code at a much finer level. As such,
it's very frustrating to be working in VBA which lacks inheritance, one of the
more powerful tools for eliminating duplication at the level I'm talking
about.
I've recently come up with a technique to emulate one...
|
by: Joe |
last post by:
We are working on a project that requires 3rd parties (ie, consultants) to
use our ASP.net webforms in a very reusable manner. The big catch is that
we are not allowed to give them source. There are a group of screens that
they all need to use in other applications, but need the ability to
customize some of the simpler behavior.
Some of the problems we are having include:
- they of course want to to be able to jump into and out of...
|
by: Massimo |
last post by:
Hi to all, I'm facing a problem in a particularly complex inheritance
hierarchy, and I'd like to know what the standard says about it and if my
compiler is correct in what it does.
I have two consecutive layers of diamond-shaped inheritance; the first layer
is declared as using virtual inheritance, while the second one is declared
as *not* using it.
This is what I'd like to have:
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |