template<class T>
T send( T &b)
{
// do something if b is a certain type
// do something else
}
Jim




Posted 18 August 2010 - 08:33 AM
template<class T>
T send( T &b)
{
// do something if b is a certain type
// do something else
}
Posted 18 August 2010 - 08:44 AM
template<class T>T send( T &B)/> //default template
{
return b;
}
template<>int send<int>(int &B)/> //case for ints
{
return b;
}
template<>char send<char>(char &B)/> //case for chars
{
return b;
}
This post has been edited by ImaSexy: 18 August 2010 - 08:45 AM
Posted 18 August 2010 - 08:45 AM
if (sizeof(val) == 4)
// this is an integer
else if (sizeof(val) == 2)
// this is a short
//etc
This post has been edited by Aphex19: 18 August 2010 - 08:45 AM
Posted 18 August 2010 - 09:15 AM
ImaSexy, on 18 August 2010 - 07:44 AM, said:
template<class T>T send( T &B)/> //default template
{
return b;
}
template<>int send<int>(int &B)/> //case for ints
{
return b;
}
template<>char send<char>(char &B)/> //case for chars
{
return b;
}
class SerialUnit : public Serial_Base
{
public:
template <class T>
T send(std::string a , T &B)/>
{
return(B)/>;
}
template<>std::string send<std::string>( std::string a,std::string &B)/>
{
return(B)/>;
}
};
Posted 18 August 2010 - 09:29 AM
class SerialUnit// : public Serial_Base
{
public:
template <class T> T send(std::string a , T &B)/>
{
return(B)/>;
}
template<> std::string send<std::string>(std::string a, std::string &B)/>
{
return(B)/>;
}
};
int main()
{
std::string a = "a", c = "c";
int d = 100;
std::cout<<send(a,c)<<std::endl
<<send(a,d)<<std::endl;
return 0;
}
This post has been edited by ImaSexy: 18 August 2010 - 09:30 AM
Posted 18 August 2010 - 10:27 AM
Posted 18 August 2010 - 01:28 PM
#include <iostream>
#include <string>
class SerialUnit// : public Serial_Base
{
public:
template <class T> T send(std::string a , T &B)/>
{
return(B)/>;
}
std::string send(const std::string a, std::string &B)/>
{ // This is to process for a string only.
return(B)/>;
}
};
int main()
{
SerialUnit test;
std::string a = "a", c = "c";
int d = 100;
std::cout<< test.send(a,c) << std::endl
<< test.send(a,d) << std::endl;
return 0;
}
Posted 18 August 2010 - 01:46 PM
#include <iostream>
#include <string>
//class SerialUnit// : public Serial_Base
//{
// public:
template <class T> T send(std::string a , T &B)/>
{
return(B)/>;
}
template<>std::string send<std::string>( std::string a,std::string &B)/>
{
return(B)/>;
}
/*
std::string send(const std::string a, std::string &B)/>
{ // This is to process for a string only.
return(B)/>;
}
*/
//};
int main()
{
// SerialUnit test;
std::string a = "a", c = "c";
int d = 100;
std::cout<< send(a,c) << std::endl
<< send(a,d) << std::endl;
return 0;
}
Posted 18 August 2010 - 01:48 PM
#include <iostream>
#include <string>
class SerialUnit
{
public:
template <class T>
std::string send(std::string, T&);
};
template <class T>
std::string SerialUnit::send(std::string, T&)
{
return "Send<T>";
}
template<>
std::string SerialUnit::send<std::string>(const std::string, std::string&)
{
return "Send<std::string>";
}
int main()
{
SerialUnit test;
std::string a = "a", c = "c";
int d = 100;
std::cout<< test.send(a,c) << std::endl
<< test.send(a,d) << std::endl;
}
This post has been edited by Bench: 18 August 2010 - 01:57 PM
Posted 18 August 2010 - 01:49 PM
Quote
#include <iostream>
#include <string>
class SerialUnit// : public Serial_Base
{
public:
template <class T> T send(std::string a , T &B)/>;
};
template <class T> T SerialUnit::send(std::string a , T &B)/>
{
return B;
}
template <> std::string SerialUnit::send<std::string>(std::string a , std::string &B)/>
{
return B;
}
int main()
{
SerialUnit test;
std::string a = "a", c = "c";
int d = 100;
std::cout<< test.send(a,c) << std::endl
<< test.send(a,d) << std::endl;
return 0;
}
This post has been edited by ImaSexy: 18 August 2010 - 01:51 PM
Posted 18 August 2010 - 01:54 PM
Posted 18 August 2010 - 03:22 PM
Posted 18 August 2010 - 03:28 PM
|
|
Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).
|
