473,386 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C++ Templates on MS VS C++

I put together a quick array template for MS Visual Studio 2003, but I
ran into some trouble. It's been quite a while since I had to roll my
own templates, so I'd appreciate all help.

Example 1
Array<int*ibuf = new Array<int>(100);
ibuf[0] = 100; //O K

Example 2
String *sptr = NULL;
Array<String**locales = new Array<String*>(50);
locales[0] = sptr; // ERROR, WHY?
(*locales)[0] = sptr; // OK, but WHY ???

Example 3 - OK: forward declaration of template class without
including template header.
template<class int8>
class Array;

Example 4 - ERROR: why ??? forward declaration of template class
without including template header.
template<class String*>
class Array;

Thank-you all!

=== ARRAY TEMPLATE ===
template<class T=int32>
class Array : public Object
{
public:
Array(int32 aSize);
virtual ~Array();

int32 length() const;

T& operator[](int32 i);
T *getData() const;

private:
T *data;
int32 size;
};

May 13 '07 #1
8 1621
On May 13, 7:06 pm, coder_...@yahoo.com wrote:
I put together a quick array template for MS Visual Studio 2003, but I
ran into some trouble. It's been quite a while since I had to roll my
own templates, so I'd appreciate all help.

Example 1
Array<int*ibuf = new Array<int>(100);
ibuf[0] = 100; //O K

Example 2
String *sptr = NULL;
Array<String**locales = new Array<String*>(50);
locales[0] = sptr; // ERROR, WHY?
(*locales)[0] = sptr; // OK, but WHY ???

Example 3 - OK: forward declaration of template class without
including template header.
template<class int8>
class Array;

Example 4 - ERROR: why ??? forward declaration of template class
without including template header.
template<class String*>
class Array;

Thank-you all!

=== ARRAY TEMPLATE ===
template<class T=int32>
class Array : public Object
{
public:
Array(int32 aSize);
virtual ~Array();

int32 length() const;

T& operator[](int32 i);
T *getData() const;

private:
T *data;
int32 size;

};
Consider something like this instead:

#include <iostream>
#include <string>

template< typename T, const size_t Size >
class Array
{
T m_a[Size];
public:
Array() { }
~Array() { }

size_t size() const { return Size; }
T& operator[](size_t i) { return m_a[i]; }
};

int main()
{
using std::string;
Array< string, 50 locales;
locales[0] = "some locale";
std::cout << "locales[0] = " << locales[0];
std::cout << std::endl;
}

/*
locales[0] = some locale
*/

Better yet, if you need a dynamic Array, use a std::vector.

May 13 '07 #2
co*******@yahoo.com wrote:
I put together a quick array template for MS Visual Studio 2003, but I
ran into some trouble. It's been quite a while since I had to roll my
own templates, so I'd appreciate all help.

Example 1
Array<int*ibuf = new Array<int>(100);
ibuf[0] = 100; //O K
The above is equivalent to:
Array<int>* ibuf = new Array<int>(100); // an array of 100 Array<int>'s

{
const Array<int>& temp(100); // a Array<intof 100 int's
ibuf[0] = temp; // assign temp to first slot in ibuf
} // destroy temp
But judging your confusion in the following context, I doubt the above
is what you really want to achieve. You might have wanted to write:
Array<intbuff(100); // array of 100 int's
buff[0] = 21;

>
Example 2
String *sptr = NULL;
Array<String**locales = new Array<String*>(50);
locales[0] = sptr; // ERROR, WHY?
Because locales is not an Array. It's a POINTER to Array. And by
convention locales[0] refers to the first Array<String*object out of
50 you have just created. So basically you are trying to assign an
Array<String*with a String*. Read on.
(*locales)[0] = sptr; // OK, but WHY ???
The * dereferences locales so the expression (*locale) is an Array, and
hence the [] operator apply.
>
Example 3 - OK: forward declaration of template class without
including template header.
template<class int8>
class Array;

Example 4 - ERROR: why ??? forward declaration of template class
without including template header.
template<class String*>
class Array;
Example 3 worked. But I suspect that it doesn't work in a way you may
have expected. Basically to forward declare a class template you do:

template <class T>
class C;

Where T is just an identifier to be used like a typename in the template
definition.

So in Example 3, the declaration is valid, you have just substituted T
with int8 and C with Array.

In Example 4, the declaration is invalid. It's because String* is an
identifier followed by a * operator.
>
Thank-you all!

=== ARRAY TEMPLATE ===
template<class T=int32>
class Array : public Object
{
Although this is ok, I'd strongly recommend you not to give a default
type parameter and not to inherit from Object. So you'd be better off by
using:

template <class T>
class Array{
public:
Array(int32 aSize);
For better portability, consider using std::size_t instead of int32.
(int32 is not a built-in type anyway.) Also, to avoid confusion you had
in Example 1, make the constructor explicit:

explicit Array(std::size_t aSize);

virtual ~Array();
If you drop the inheritance, then virtual destructor is not needed.
>
int32 length() const;

T& operator[](int32 i);
For const use, also provide:

const T& operator[](std::size_t) const;
T *getData() const;
The above member function is ill-declared. Consider instead:

T* getData();
const T* getData() const;

>
private:
T *data;
int32 size;

Before you finish the class, make sure you also provide a copy
constructor and an assignment operator:

template <class U>
Array(const Array<U>&);

template <class U>
Array<T>& operator=(const Array<U>&);

};
Regards,
Ben
May 14 '07 #3
Hi Ben,

You got me worried :)

Per your example,
Array<intbuff(100); // array of 100 int's

Yes, that's what I want. An array of 100 ints. However, I'd like to
allocate the array of 100 ints dynamically with new. I thought that
Array<int*buf = new Array<int>(100) was the right way to go. Sorry
for asking this basic question, but how do I allocate an array of ints
using new with my template, so the memory allocated will be on the
heap? Does the quesiton make sense at all?

Thanks!!!

On May 13, 10:07 pm, benben <benhonghatgmaildotcom@nospamwrote:
coder_...@yahoo.com wrote:
I put together a quick array template for MS Visual Studio 2003, but I
ran into some trouble. It's been quite a while since I had to roll my
own templates, so I'd appreciate all help.
Example 1
Array<int*ibuf = new Array<int>(100);
ibuf[0] = 100; //O K

The above is equivalent to:

Array<int>* ibuf = new Array<int>(100); // an array of 100 Array<int>'s

{
const Array<int>& temp(100); // a Array<intof 100 int's
ibuf[0] = temp; // assign temp to first slot in ibuf

} // destroy temp

But judging your confusion in the following context, I doubt the above
is what you really want to achieve. You might have wanted to write:

Array<intbuff(100); // array of 100 int's
buff[0] = 21;
Example 2
String *sptr = NULL;
Array<String**locales = new Array<String*>(50);
locales[0] = sptr; // ERROR, WHY?

Because locales is not an Array. It's a POINTER to Array. And by
convention locales[0] refers to the first Array<String*object out of
50 you have just created. So basically you are trying to assign an
Array<String*with a String*. Read on.
(*locales)[0] = sptr; // OK, but WHY ???

The * dereferences locales so the expression (*locale) is an Array, and
hence the [] operator apply.
Example 3 - OK: forward declaration of template class without
including template header.
template<class int8>
class Array;
Example 4 - ERROR: why ??? forward declaration of template class
without including template header.
template<class String*>
class Array;

Example 3 worked. But I suspect that it doesn't work in a way you may
have expected. Basically to forward declare a class template you do:

template <class T>
class C;

Where T is just an identifier to be used like a typename in the template
definition.

So in Example 3, the declaration is valid, you have just substituted T
with int8 and C with Array.

In Example 4, the declaration is invalid. It's because String* is an
identifier followed by a * operator.
Thank-you all!
=== ARRAY TEMPLATE ===
template<class T=int32>
class Array : public Object
{

Although this is ok, I'd strongly recommend you not to give a default
type parameter and not to inherit from Object. So you'd be better off by
using:

template <class T>
class Array{
public:
Array(int32 aSize);

For better portability, consider using std::size_t instead of int32.
(int32 is not a built-in type anyway.) Also, to avoid confusion you had
in Example 1, make the constructor explicit:

explicit Array(std::size_t aSize);
virtual ~Array();

If you drop the inheritance, then virtual destructor is not needed.
int32 length() const;
T& operator[](int32 i);

For const use, also provide:

const T& operator[](std::size_t) const;
T *getData() const;

The above member function is ill-declared. Consider instead:

T* getData();
const T* getData() const;
private:
T *data;
int32 size;

Before you finish the class, make sure you also provide a copy
constructor and an assignment operator:

template <class U>
Array(const Array<U>&);

template <class U>
Array<T>& operator=(const Array<U>&);
};

Regards,
Ben

May 14 '07 #4
co*******@yahoo.com wrote:
Hi Ben,

You got me worried :)

Per your example,
Array<intbuff(100); // array of 100 int's

Yes, that's what I want. An array of 100 ints. However, I'd like to
allocate the array of 100 ints dynamically with new. I thought that
Array<int*buf = new Array<int>(100) was the right way to go. Sorry
for asking this basic question, but how do I allocate an array of ints
using new with my template, so the memory allocated will be on the
heap? Does the quesiton make sense at all?

Thanks!!!
Well, the Array class template you wrote should handle the memory
allocation by itself, right?

Given that:

template <typename T>
class Array
{
private:
T* data;
std::size_t size;

// ...
};

Just question your self what you are going to do with member variable
Array<T>::data. Details vary from design to design but in most case data
is initiated (which typically means made point to an allocated memory)
in the constructor. And it is properly destroyed in the destructor.
Therefore, when you create an Array<intinstance, for example:

Array<intbuff(100);

The size 100 is passed into the constructor Array<int>::Array(size_t)
and it will from there new 100 int's and point data to there.

Whatever your design is, the followings definitely are not what you
would expect:

Array<int>* pbuff = new Array<int>(100); // one Array of 100 int's
// on free store
(*pbuff)[99] = 1;
Array<int>* buffs = new Array<int>[100]; // 100 Arrays each holding
// a default number of ints
int s = buffs[99].length(); // length of 100th Array<int>

delete[] buffs;
delete pbuff;

// etc

Regards,
Ben
May 14 '07 #5
Now, I feel less worried :) I think you may have misread my original
code sample :)

Array<int>* pbuff = new Array<int>(100); // one Array of 100 int's
// on free store
(*pbuff)[99] = 1;

That's what I wanted and need, but why do I need the clunky (*pbuff)
[99] = 1 construct? Is there a way to setup the template,
so I can just do a pbuff[99] = 1 ?

Thanks...

On May 14, 2:54 am, benben <benhonghatgmaildotcom@nospamwrote:
coder_...@yahoo.com wrote:
Hi Ben,
You got me worried :)
Per your example,
Array<intbuff(100); // array of 100 int's
Yes, that's what I want. An array of 100 ints. However, I'd like to
allocate the array of 100 ints dynamically with new. I thought that
Array<int*buf = new Array<int>(100) was the right way to go. Sorry
for asking this basic question, but how do I allocate an array of ints
using new with my template, so the memory allocated will be on the
heap? Does the quesiton make sense at all?
Thanks!!!

Well, the Array class template you wrote should handle the memory
allocation by itself, right?

Given that:

template <typename T>
class Array
{
private:
T* data;
std::size_t size;

// ...
};

Just question your self what you are going to do with member variable
Array<T>::data. Details vary from design to design but in most case data
is initiated (which typically means made point to an allocated memory)
in the constructor. And it is properly destroyed in the destructor.
Therefore, when you create an Array<intinstance, for example:

Array<intbuff(100);

The size 100 is passed into the constructor Array<int>::Array(size_t)
and it will from there new 100 int's and point data to there.

Whatever your design is, the followings definitely are not what you
would expect:

Array<int>* pbuff = new Array<int>(100); // one Array of 100 int's
// on free store
(*pbuff)[99] = 1;

Array<int>* buffs = new Array<int>[100]; // 100 Arrays each holding
// a default number of ints
int s = buffs[99].length(); // length of 100th Array<int>

delete[] buffs;
delete pbuff;

// etc

Regards,
Ben- Hide quoted text -

- Show quoted text -

May 15 '07 #6
co*******@yahoo.com wrote:
Now, I feel less worried :) I think you may have misread my original
code sample :)

Array<int>* pbuff = new Array<int>(100); // one Array of 100 int's
// on free store
(*pbuff)[99] = 1;

That's what I wanted and need, but why do I need the clunky (*pbuff)
[99] = 1 construct? Is there a way to setup the template,
so I can just do a pbuff[99] = 1 ?

Thanks...
Sure! Just do:

Array<intbuff(100); // Array of 100 ints
buff[99] = 66;

Regards,
Ben
May 15 '07 #7
We've gone full circle and my original question remains though :)
I'll will have to chunk out a full compilable sample to illustrate :)

Speaking of which, with templates, can you do these kind of
initialization?

int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};

Thanks!!!

On May 15, 4:10 am, benben <benhonghatgmaildotcom@nospamwrote:
coder_...@yahoo.com wrote:
Now, I feel less worried :) I think you may have misread my original
code sample :)
Array<int>* pbuff = new Array<int>(100); // one Array of 100 int's
// on free store
(*pbuff)[99] = 1;
That's what I wanted and need, but why do I need the clunky (*pbuff)
[99] = 1 construct? Is there a way to setup the template,
so I can just do a pbuff[99] = 1 ?
Thanks...

Sure! Just do:

Array<intbuff(100); // Array of 100 ints
buff[99] = 66;

Regards,
Ben

May 16 '07 #8
Speaking of which, with templates, can you do these kind of
initialization?

int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
None. But rumor has it they're gonna make that happen in C++0x.
>
Thanks!!!
Ben
May 18 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vince C. | last post by:
Hi all, I've created XML documents that are described with a schema. I'm using those documents to create web pages. All my web pages contain a fixed header and a variable document part. The...
5
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
22
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
12
by: Fabio De Francesco | last post by:
Hello. I can't understand why I can't compile the following simple code, where I think I have applied all the needed rules for templates that are declared and defined in different files (*.h and...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
2
by: jimbo_vr5 | last post by:
Hey I think i've figured out the idea behind apply-templates. But going through the tutorial on <http://www.w3schools.com/xsl/xsl_apply_templates.asp> theres simply just something that i dont...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
7
by: Chris | last post by:
Hi All, This is a weird one but I am hoping someone can help or has some pointers, a recipe how to do the following: I have to move some code from c++ to objective-c and to do this I must...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.