Jump to content
  • Advertisement
Sign in to follow this  
Axiverse

Array Template Problems (byref/byval)

This topic is 4749 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 have recentally created a Array Template, but there is one thing that does not work, and that is that sove function parameters are passed by reference and not by value. If it has a type that cannot be converted to a reference (e.g. double, int) there is a compiler error. How can I fix this, do I need to make a specialized version for each value type (might then be easier to just make a seperate template), or what can i do? Is there a if value then "void Add(type);" else "void Add(type&);"?

Share this post


Link to post
Share on other sites
Advertisement
I'm going to take a stab in the dark and guess that your problem is that you have something like the following:
template < typename TYPE >
class array
{

public:

void add(TYPE & value)
{
// add the value to the array
}

};

int main()
{
array< float > myarray;
myarray.add(1.337f);
}

The problem here is const-correctness. 1.337f is a temporary, and as such can only be converted to a const reference to float, not a non-const reference to float. The second choice solution is to correct your add member function to be const correct:
void add(TYPE const & value)
{
// add the value to the array
}

The first choice solution (except for learning purposes) is to just use a standard container such as std::vector or std::deque.

Enigma

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!