template question

I

Iguana

Say you've got this class....


<template TYPE>
class CNode {
TYPE data;
};

How would you initialise 'data'? I have to admitt I've not been programming
as much as I did when I was younger, looking at the MFC class 'CLink' it
appears to be possible, although the CList class is template and contains
the class CNode that isn't template.

I stupidly started with operator= before I remembered it wasn't defined yet,
then thought about using pointers to copy the data, but sizeof TYPE and
sizeof(TYPE) don't seem to be able to be evaluated.

Thanks in advance,
Iguana
 
T

Thomas Matthews

Iguana said:
Say you've got this class....


<template TYPE>
class CNode {
TYPE data;
};

How would you initialise 'data'?
For intrinsic types, such as integer:
CNode<int> my_node = {15};

If the type is a user-defined type, it must
have an initialization constructor or the assignment
operator defined.

class Frog
{
int croaks_per_night;
public:
Frog(int max_croaks = 15)
: croaks_per_night(max_croaks)
{ }
};

CNode<Frog> frog_node = {Frog_Node(26)};

If I'm wrong, somebody will correct me; which is
the good thing about newsgroups.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
B

Bob Hairgrove

Say you've got this class....


<template TYPE>
class CNode {
TYPE data;
};

How would you initialise 'data'?

You need to define a constructor, i.e.:

<template typename TYPE>
class CNode {
public:
CNode() : data() { /*...*/ }
private:
TYPE data;
};

This will default-initialize data. Provide additional constructors
taking a TYPE argument to initialize "data" with something else.
 
I

Iguana

For intrinsic types, such as integer:
CNode<int> my_node = {15};

If the type is a user-defined type, it must
have an initialization constructor or the assignment
operator defined.

template <class TYPE>
class CNode
{
TYPE data;
};

In this case, it doesn't seem to show how TYPE can possibly (in ALL cases)
have a non-default constructor or operator=(const TYPE &). If there is a
way to show what properties the template class TYPE has tho, it would work.
I don't see how it would be possible tho. Again there's something I'm
missing here cos it appears to be able to be done with CLink in the MFC
libraries, and it is probably totally simple ;)

I am using Visual C++ .net 2003 if that's the problem

Agreed tho, its dead easy with, for example, an int, just not an abstract
class as far as I can see

A few things I've tried since...

template <class T : public CInherited>
....

template <class TYPE>T &Reference() { return m_pNode->data; }

where CInherited has an operator=() defined in it as pure virtual
 
I

Iguana

Sorry about the double take, hit CTRL-ENTER by mistake

A few things I've tried since...

template <class TYPE : public CInherited>
...

where CInherited has an operator=() defined in it as pure virtual.
(doesn't compile)


Also....

template <class TYPE>
class CNode {
TYPE data;
public:
TYPE &GetReference() { return data; }
};

Which leaves the user responsible for altering the data with a dodgy
function that I believe is bad practice and so I'm a little hesitant with
this
Can't use a operator=() or Set() as it isn't known how to copy items of
class TYPE

What would be perfect is it being the same as CLink. That is that it has
clean functions such as AddHead(TYPE) and AddTail(TYPE) in:- template<class
TYPE,class ARG_TYPE = TYPE&> class CLink
 
P

Peter Koch Larsen

Iguana said:
Say you've got this class....


<template TYPE>
class CNode {
TYPE data;
};

How would you initialise 'data'? I have to admitt I've not been
programming
as much as I did when I was younger, looking at the MFC class 'CLink' it
appears to be possible, although the CList class is template and contains
the class CNode that isn't template.

I stupidly started with operator= before I remembered it wasn't defined
yet,
then thought about using pointers to copy the data, but sizeof TYPE and
sizeof(TYPE) don't seem to be able to be evaluated.

Thanks in advance,
Iguana
As Bob said, you should probably define a constructor. If not, you can only
have default constructed values in a CNode - not much fun! Operator= is
defined, though - implicitly by the compiler. To verify this, try:
CNode<int> a,b;
a = b;
One last remark: DO NOT USE UPPERCASE ONLY FOR NON-MACRO TYPES. UPPERCASE IS
RESERVED FOR MACROS ONLY. This is not a language rule, just very good
advice.

/Peter
 
I

Iguana

As Bob said, you should probably define a constructor. If not, you can
only have default constructed values in a CNode - not much fun! Operator=
is defined, though - implicitly by the compiler. To verify this, try:
CNode<int> a,b;
a = b;

Thanks for the help here :). Figured out what I was doing wrong not long
before reading it...

void CNode<class A, class B>::eek:perator=(***)
{
}

instead of (as you've probably guessed)...

template<class A,class B>
void CNode<A,B>::eek:perator=(***)
{
}

Serves me right for staying up till 6am
One last remark: DO NOT USE UPPERCASE ONLY FOR NON-MACRO TYPES. UPPERCASE
IS RESERVED FOR MACROS ONLY. This is not a language rule, just very good
advice.

/Peter

I wasn't sure about this, cos I got the all uppercase from the microsoft
header files. So I checked the net and found someone saying the naming
convention is a single capital letter (class D, class T). I would be
interested in what you use here and why?

On the same page however it says... Coding standards and guidelines tend to
start "religious wars"... so I'm happy so long as I'm using someone else's
convention and using it consistantly ;)

Thanks again ;),
Iguana
 
P

Peter Koch Larsen

Hi Iguana

Sorry 'bout my late reply. Just saw your post now.
Iguana said:
Thanks for the help here :). Figured out what I was doing wrong not long
before reading it...

void CNode<class A, class B>::eek:perator=(***)
{
}

instead of (as you've probably guessed)...

template<class A,class B>
void CNode<A,B>::eek:perator=(***)
{
}

Serves me right for staying up till 6am


I wasn't sure about this, cos I got the all uppercase from the microsoft
header files. So I checked the net and found someone saying the naming
convention is a single capital letter (class D, class T). I would be
interested in what you use here and why?

That is their naming convention, and it is allright with me. As long as its
not all CAPITALS, I'm quite happy.
On the same page however it says... Coding standards and guidelines tend
to start "religious wars"... so I'm happy so long as I'm using someone
else's convention and using it consistantly ;)

I do not care much about naming - most are intelligible to me. One nice
guide is
http://cvs.sourceforge.net/viewcvs....l?rev=1.1.2.5&only_with_tag=coding_guidelines
Avoiding the Microsoft naming convention has one advantage though: you do
not risk confusing your compiler if you accidently happen to name one of
your function the same way as some microsoft function that has been defined
with Unicode portability in mind.
Thanks again ;),
Iguana
/Peter
 

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


Members online

Forum statistics

Threads
474,184
Messages
2,570,979
Members
47,578
Latest member
LC_06

Latest Threads

Top