Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

template question 1

Status
Not open for further replies.

rsshetty

Programmer
Dec 16, 2003
236
US
Hi,

I am new to the implementation of templates.

Theoretically, this is what I need.

class A
{
somefunction(int [][2]);
somefunction(float [][2]);

otherfunction1();
otherfunction2();
}
I want to create a function template for somefunction. I can create a template but I am not sure how to call it and also how to define the prototype in class A.

Thanks.



rsshetty.
It's always in the details.
 
The best way to learn how to implement classes in c++ is throught an example. Today, it will be cFrog:

Code:
class cFrog{

//public variables, or the class properties and methods go under public
public:
int weight;
const char* name;

cFrog(); //constructor definition
~cFrog(); //deconstructor definition
void die();
void hop(int iHeight);

//Protected variables that only the class itself can access go under private
private: 
bool bAlive;

};

//Thats the end of the class definition, now we define the methods

//Constructor and deconstructor has no type
cFrog::cFrog(){
//initialize starting values in constructor
weight = 5;
name = "Froggie";
bAlive = 1;
}

cFrog::~cFrog(){
//any clean up goes in here
}

void cFrog::hop(int iHeight){
printf("hop");
}

void cFrog::die(){
bAlive = 0;
}

//end

As for prototype objects, c++ doesn't use a prototype system, but a more traditional inheritance system. Lets say i want to make a class "cFrogger" that is devried from cFrog. This is how i'd do it:

Code:
class cFrogger public cFrog{
//class definition
};

Now frogger inherits all the properties and methods from cFrog. Now to create an instance of our cFrogger we just do:

Code:
oFrog = new cFrogger();

Be sure u delete class instances after u create them and before your program closes:

Code:
delete oFrog;

Hope that helps, and I hope i didnt mess up too badly with my example.
 
Just do it like this.

Code:
class A
{
public:
   template<class T> int somefunction(T [][2]);
};

template<class T>
int A::somefunction(T [][2])
{
	return 0;
}

To call it just call a function like normal and it will generate all of the different types. ie, if you call somefunction and pass it a double array, it will use the double version, if you pass it an int array, it will use the int version, etc.
 
Thanks timmay3141. That's what I wanted.

rsshetty.
It's always in the details.
 
fugigoose: You must have answered in the wrong thread or something. If you haven't already, post your response in the thread whose question you are attempting to answer so the person who asked can actually find it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top