HighTechTalks DotNet Forums  

C++, unmanaged code

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss C++, unmanaged code in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Bill S.
 
Posts: n/a

Default C++, unmanaged code - 07-06-2006 , 03:20 PM






Dear programmers, I'm experimenting with a simple unmanaged template class.
The problem is that it won't except managed types. Any suggestions?

void MyFunc()
{
//This works fine.
XStack<int>* uc = new XStack<int>();

//This doesn't work!
XStack<ManagedClass*>* uc = new XStack<ManagedClass*>();
}

//Unmanaged Class ////////////////////
template<class Type> class XStack
{
private:
typedef struct StackNode{
Type Data;
StackNode *Next;
}mNode;

bool mEmpty;
int mSize;
mNode *mHeader;

public:
XStack()
{
mEmpty = true;
mSize = 0;
mHeader = new mNode;
mHeader->Data = 0;
mHeader->Next = 0;
}

~XStack()
{
while( !mEmpty ){
Pop();
}
//delete mHeader; //?
}

bool empty() const
{
return mEmpty;
}

int size() const
{
return mSize;
}

Type Pop()
{
if( !mEmpty ){
mNode *tmp = mHeader->Next;
mHeader->Next = tmp->Next;
mEmpty = (mHeader->Next == 0);
mSize--;
Type data = tmp->Data;
//delete tmp; //?
return data;
}
}

void Push( Type data )
{
mEmpty = false;
mSize++;
mNode *tmp = new mNode;
tmp->Data = data;
tmp->Next = mHeader->Next;
mHeader->Next = tmp;
}
};



Reply With Quote
  #2  
Old   
Debasish Bose, Oracle Corp
 
Posts: n/a

Default RE: C++, unmanaged code - 07-07-2006 , 06:08 AM






Use gcroot<> instead.

"Bill S." wrote:

Quote:
Dear programmers, I'm experimenting with a simple unmanaged template class.
The problem is that it won't except managed types. Any suggestions?

void MyFunc()
{
//This works fine.
XStack<int>* uc = new XStack<int>();

//This doesn't work!
XStack<ManagedClass*>* uc = new XStack<ManagedClass*>();
}

//Unmanaged Class ////////////////////
template<class Type> class XStack
{
private:
typedef struct StackNode{
Type Data;
StackNode *Next;
}mNode;

bool mEmpty;
int mSize;
mNode *mHeader;

public:
XStack()
{
mEmpty = true;
mSize = 0;
mHeader = new mNode;
mHeader->Data = 0;
mHeader->Next = 0;
}

~XStack()
{
while( !mEmpty ){
Pop();
}
//delete mHeader; //?
}

bool empty() const
{
return mEmpty;
}

int size() const
{
return mSize;
}

Type Pop()
{
if( !mEmpty ){
mNode *tmp = mHeader->Next;
mHeader->Next = tmp->Next;
mEmpty = (mHeader->Next == 0);
mSize--;
Type data = tmp->Data;
//delete tmp; //?
return data;
}
}

void Push( Type data )
{
mEmpty = false;
mSize++;
mNode *tmp = new mNode;
tmp->Data = data;
tmp->Next = mHeader->Next;
mHeader->Next = tmp;
}
};




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.