C++ Templates on MS VS C++

C

coder_lol

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;
};
 
S

Salt_Peter

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; }
};

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.
 
B

benben

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<int> of 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<int> buff(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 said:
(*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:

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>

Regards,
Ben
 
C

coder_lol

Hi Ben,

You got me worried :)

Per your example,
Array<int> buff(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!!!

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<int> of 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<int> buff(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 said:
(*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:

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 said:

Regards,
Ben
 
B

benben

Hi Ben,

You got me worried :)

Per your example,
Array<int> buff(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<int> instance, for example:

Array<int> buff(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
 
C

coder_lol

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...

You got me worried :)
Per your example,
Array<int> buff(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<int> instance, for example:

Array<int> buff(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 -
 
B

benben

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<int> buff(100); // Array of 100 ints
buff[99] = 66;

Regards,
Ben
 
C

coder_lol

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!!!

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<int> buff(100); // Array of 100 ints
buff[99] = 66;

Regards,
Ben
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Lexical Analysis on C++ 1
templates?? 2
Parking lot C# 5
Templates and g++ 4
C++ Syntax Confusion 4
templates 10
Custom matrix multiplication produces different results to glm 0
Memory : Stack vs Heap 9

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top