Jump to content
  • Advertisement
Sign in to follow this  
stylin

Static Const Members and Templates

This topic is 4842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The relevant code for a Smart Pointer class is as follows:
Quote:
template<class O>
class SP
{
private:
	O*	obj;
public:
	// default constructor: initializes our SP to null
	SP() : obj(0), refcount(0) {};

	// copy constructor: initializes our SP to point to an existing object
	SP( const O& object ) : obj( p2object )	{};

	// copy constructor: initializes our SP to point with an existing SP
	SP( const SP<O>& sp2object ) : obj( sp2object->obj ) {};

	// equality operator:
	bool operator==( const SP<O>& object ) const {};

	static const SP<O> spNULL;
};

template <typename O> const SP<O> SP<O>::spNULL;
A couple of questions I have: 1. Is the static member useful, or should I just compare to 0? 2. How could I create a const null smart pointer outside the class definition? 3. Can I typedef parameterized class types? Thanks in advance to anyone who has info on this. :stylin:

Share this post


Link to post
Share on other sites
Advertisement
Well to answer your third question, you can't. Assuming I understand your question correctly, that is. You want to know if you can typedef a std::vector as say, an Array, and be able to do Array<T>?

Share this post


Link to post
Share on other sites
@ITGER: I want to do this:

template< typename O >
typedef class SP {} SmartPointer;

But I get this:
...\main.cpp(34) : error C2059: syntax error : '<end Parse>'


Help tells me this:
Quote:
If the error message occurs on a symbol immediately following a typedef'd variable, ensure that the variable has been defined somewhere in the source code.


I'm using Visual C++ v6.0

:stylin:

Share this post


Link to post
Share on other sites
1. Doesn't seem useful to me.
2. Not sure what you are asking.
3. No, templated typedefs are not part of the language, though they may be added in the next revision of the standard.

Share this post


Link to post
Share on other sites
VC6's template support is so bad that even if C++ had template typedef support (it doesn't - yet), VC6 wouldn't have it. You just can't do that, period.

Regarding your other questions
1) I don't see a comparison, so I can't judge.
2) There are many ways that question can be interpreted. Care to be more specific?

Share this post


Link to post
Share on other sites
Thanks SiCrane, guess I'll just have to rename my classes.

Also, I wondered if I can have a stand-alone constant defined in the global namespace that relies on an existing template:
template<typename O> const SP<O> spNULL=0;

Or am I forced to use class scope?

:stylin:

Share this post


Link to post
Share on other sites
That won't work - the template parameter is made part of the constant's type, not of its name. If I just write spNULL, which one am I referring to?

Share this post


Link to post
Share on other sites
As SiCrane and Fruny have already pointed out, you can't have a templated typedef currently in C++. The closest you can get is something of the form:
template < typename TYPE >
struct SmartPointer
{
typedef SP< TYPE > type;
};

SmartPointer< int >::type smartPointerToInt;
SmartPointer< std::list< char > >::type smartPointerToListOfChar;

Obviously you can change the names, i.e. Smart< TYPE >::Pointer, Type< TYPE >::SmartPointer etc.
You can also embed the smartpointer typedef in your own classes, although this is intrusive:
class MyClass
{
public:
typedef SP< MyClass > SmartPointer;
};

MyClass::SmartPointer smartPointerToMyClass;

You can also make a mixin base class to do this for you, although it doesn't save you much:
template < typename TYPE >
class SmartPointerTypedef
{
public:
typedef SP< TYPE > SmartPointer;
};

class MyClass
:
public SmartPointerTypedef< MyClass >
{
};

MyClass::SmartPointer smartPointerToMyClass;

Enigma

Share this post


Link to post
Share on other sites
@Fruny: So there's no way I can do something like this:

template<typenameO> const SP<O> spNULL;

...

SP<Entity> entity( oldEnt ); // use entity
...
entity = static_cast< SP<Entity> >(spNULL); // zero entity

@Enigma: Clever stuff - I'll definitely take a look at it. Thanks.

:stylin:

EDIT: typos

Share this post


Link to post
Share on other sites
Quote:
Original post by stylin
@Fruny: So there's no way I can do something like this:

template<typenameO> const SP<O> spNULL;

...

SP<Entity> entity( oldEnt ); // use entity
...
entity = static_cast< SP<Entity> >(spNULL); // zero entity


No, but as spNULL is static you can do this:
SP<Entity>::spNULL;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!