Skip to main content
GameDev.net gamedev.net
🔒 Locked

Array Template Problems (byref/byval)

Started by Axiverse Sep 23, 2005 at 3:08 PM 0 replies 350+ views
Original Post
Axiverse
Axiverse
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&);"?
Enigma
Enigma
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.