Reputation: 5
So i want to declare a function func(temp t1, temp t2) and use it in class temp. But i do not want to define it as a member function of the class temp mainly because i want other functions to be able to access func without using any object of temp. I know it is possible by declaring func() as a friend to temp but is there a way to declare a sort of prototype for temp so that I can use it as arguments for a non member function temp and then define it later?
template<typename Type>
class record
{
public:
Type data;
/*....some other memebers...*/
friend int rec_comp(const record& r1, const record& r2)
{
}
bool operator==(const record& r1, const record& r2)
{
if(rec_comp(r1, r2)==0)
return true;
else
return false;
}
/*...similar implementation for other relational operators ...*/
};
i want to declare the function rec_comp outside the class.
Upvotes: 0
Views: 1356
Reputation: 1
Yes it is possible. One example is given below:
func.h
#pragma once
//declaration for class temp
class Temp;
//declaration for function func
void func(Temp t1, Temp t2);
func.cpp
#include "func.h"
#include "temp.h"
#include<iostream>
//implementation for function func
void func(Temp t1, Temp t2)
{
std::cout<<"func called"<<std::endl;
//do something with t2 and t2
}
temp.h
#pragma once
//class definition
class Temp
{
int i = 0;
public:
//default constructor
Temp();//this is a declaration
};
temp.cpp
#include "temp.h"
#include <iostream>
//definition for default constructor
Temp::Temp()
{
std::cout<<"default constructor called"<<std::endl;
}
main.cpp
#include <iostream>
#include "func.h"
#include "temp.h"
int main()
{
Temp t1, t2;
//call func
func(t1,t2);
return 0;
}
The output of the above program can be seen here.
Since you have edited your question, below is the answer to your edited question.
template<typename Type>
class record
{
public:
Type data;
//NOTE: rec_comp is NOT A MEMBER FUNCTION
template<typename U, typename V> friend int rec_comp(const record<U>& r1, const record<V>& r2); //THIS IS A DECLARATION
};
//THIS IS A DEFINITION
template<typename U, typename V>
int rec_comp(const record<U>& r1, const record<V>& r2)
{
return 5;
}
Upvotes: 0
Reputation: 238311
Is there a way to declare a clss and then define it later in c++?
Yes. Simply omit the curly bracket delimited body of the class definition to get a non-defining declaration. This is declaration of a class:
class temp;
declaring func() as a friend
Declaring function as a friend of class is only useful in allowing the friend function to access protected and private members and bases of the class.
So i want to declare a function func(temp t1, temp t2) and use it in class temp.
Declaring the class (without definition) before declaring the function would indeed work. But it isn't strictly necessary. Alternative order that also works:
temp
func
temp
that use func
However, I do recommend the approach of declaring the class first. It's usually easiest to find a correct order by following the pattern of declare classes, declare functions, define classes, define functions.
Upvotes: 1