Jump to content
  • Advertisement
Sign in to follow this  
BeerNutts

C++ template member function question...

This topic is 2564 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

I fear I know the answer to this, but I wanted to be sure since I'm still not 100% sure about templates.

I have a class, and I want to be able to store and retrieve some data of any type, but I don't want to have to re-declare my class as a template since this is a minor piece of the class.

So, I'd have this

class MyClass
{
public:
MyClass() {}
~MyClass() {}

template <class T>
void SetData(T &Data);

template <class T>
void GetData(T&Data);

private:
T &mData;
}


I'm pretty sure it's not possible to do it the way i want, but are there other options I haven't looked it?

Thanks

Share this post


Link to post
Share on other sites
Advertisement

I fear I know the answer to this, but I wanted to be sure since I'm still not 100% sure about templates.

I have a class, and I want to be able to store and retrieve some data of any type, but I don't want to have to re-declare my class as a template since this is a minor piece of the class.

So, I'd have this

class MyClass
{
public:
MyClass() {}
~MyClass() {}

template <class T>
void SetData(T &Data);

template <class T>
void GetData(T&Data);

private:
T &mData;
}


I'm pretty sure it's not possible to do it the way i want, but are there other options I haven't looked it?

Thanks


You can have member functions that are templatized like that (as long as you include the implementation in the header), but if you want that member variable to be determined by the template type, that requires the class to be templatized.

-Josh

Share this post


Link to post
Share on other sites

I'm pretty sure it's not possible to do it the way i want, but are there other options I haven't looked it?

In general, the fact that you think you need to store data of an arbitrary type, is likely to indicate a gaping whole in your program design.

But if you really, really have to... then don't reinvent the wheel.

Share this post


Link to post
Share on other sites

Hello.
You should better write your code as below if you just one to store the date in your class.[size=2]

template <class T>
class MyClass
{
public:
MyClass() {}
~MyClass() {}

void SetData(const T &Data);

void GetData(T &Data) const;

private:
T mData;
};


template <class T>
void MyClass::SetData(const T &Data)
{
mData = Data;
}


template <class T>
void MyClass::GetData(T &Data) const
{
Data = mData
}


EDIT: GetDate() member mustn't be constant. You must fix that mistake in the code above. Edited by hkBattousai

Share this post


Link to post
Share on other sites


Hello.
You should better write your code as below if you just one to store the date in your class.[size=2]

template <class T>
class MyClass
{
public:
MyClass() {}
~MyClass() {}

void SetData(const T &Data);

void GetData(T &Data) const;

private:
T mData;
};


template <class T>
void MyClass::SetData(const T &Data)
{
mData = Data;
}


template <class T>
void MyClass::GetData(T &Data) const
{
Data = mData
}



Thanks, but the point was doing that WITHOUT having the class be declared as a template.

Share this post


Link to post
Share on other sites

Thanks, but the point was doing that WITHOUT having the class be declared as a template.


OK, then. If that was the point, you can use your original code as well.
But, are you sure that you want to save a reference to mData instead of the mData itself?

Also, I see that you defined mData as the T type. But it will give a compiler error since T is not defined inside MyClass. This is a problem you should somehow overcome if you don't want to use a template class.

Share this post


Link to post
Share on other sites
Do you actually need to be able to store any type, or just a set of types (typically something like {int, float, string, bool})?

Can you describe what this class is or how it would be used?

Share this post


Link to post
Share on other sites
This smells like a design problem, so I'm not recommending you actually do this, but you could just make mData a void*.

Right now, it looks like you're just writing a clumsy wrapper for the C++ type system.

Share this post


Link to post
Share on other sites

This smells like a design problem, so I'm not recommending you actually do this, but you could just make mData a void*.

Right now, it looks like you're just writing a clumsy wrapper for the C++ type system.


The class above is just an example, it's actually an Event system, where the client sends an event to a register listener. I was looking at adding a data component to the event.

I currently am using a void*, but I thought a template solution would've been more elegant solution, so I was trying to add it in with out redefining the class.

Share this post


Link to post
Share on other sites

I'm not recommending you actually do this, but you could just make mData a void*.

I'm going to reiterate this: if you think you need a void * you have a design problem. But if you can't fix your design problem, then boost::any is much preferable to a void * pointer.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!