[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: a compiation issue with gcc
From: |
Eljay Love-Jensen |
Subject: |
Re: a compiation issue with gcc |
Date: |
Thu, 19 May 2005 08:13:56 -0500 |
Hi Damanjit,
I see a few unrelated problems with your code.
1)
#include "iostream.h" is not C++ compliant (it's deprecated at best). Use
#include <iostream>. Since it wasn't used in your code example, I removed it.
2)
#include "stdio.h" is not C++ compliant (it's deprecated at best). Use
#include <cstdio>. Since it wasn't used in your code example, I removed it.
3)
With GCC, you should compile C++ code with the g++ front end, not with gcc.
Now on to the meat of the problem.
4)
You appear to be replicating std::auto_ptr, which is part of Standard C++ in
the C++ <memory> header. But maybe you have a simplified example for
illustrative purposes, so just consider this an FYI.
5)
You were missing the header for free(). I added #include <cstdlib>. Instead
of using <cstdlib>'s malloc / free for your heap memory management, I strongly
recommend using C++ new / delete. But that may be an aside from your actual
problem at hand.
6)
You are passing in a function pointer as a template type parameter. I vaguely
recall there can be issues with function pointers (especially ones that are
static-at-the-translation-unit level, or static-at-the-class-level).
As an alternative for you consideration (which will be more digestible / less
problematic amongst a variety of C++ compilers at different levels of C++
compliance), you could use a functor object. Functor objects tend to be a lot
more C++ friendly in general, anyway -- but may or may not be suitable for your
needs.
That could look something like this:
--------------------------------------
#include <cstdlib> // free
template <class T, class DestroyFunctor>
class B
{
private:
T a;
};
template <class T>
class A
{
private:
void operator ()(T Ptr)
{
free(Ptr);
}
public:
typedef B<T, A<T> > Type;
};
int main()
{
A<int*> a;
return 0;
}
--------------------------------------
HTH,
--Eljay