有没有一种方法可以声明一个 class 然后稍后在 C++ 中定义它?
Is there a way to declare a class and then define it later in c++?
所以我想声明一个函数 func(temp t1, temp t2) 并在 class temp 中使用它.但是我不想将它定义为 class temp 的成员函数,主要是因为我希望其他函数能够访问 func 不使用 temp 的任何对象。我知道可以通过将 func() 声明为 temp 的朋友来实现,但是有没有办法为 [=16 声明一种原型=]temp 以便我可以将它用作非成员函数的参数 temp 然后稍后定义它?
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 ...*/
};
我想在 class.
之外声明函数 rec_comp
Is there a way to declare a clss and then define it later in c++?
是的。只需省略 class 定义的大括号分隔主体即可获得非定义声明。这是 class:
的声明
class temp;
declaring func() as a friend
将函数声明为 class 的友元仅在允许友元函数访问 class.
的受保护和私有成员和基类时有用
So i want to declare a function func(temp t1, temp t2) and use it in class temp.
在声明函数之前声明 class(没有定义)确实可行。但这不是绝对必要的。同样有效的替代顺序:
- 定义
temp
- 声明
func
- 定义使用
func
的 temp
的成员函数
不过,我推荐先声明 class 的方法。按照声明 classes,声明函数,定义 classes,定义函数的模式通常最容易找到正确的顺序。
是的,这是可能的。举例如下:
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;
}
上面程序的输出可见here.
编辑
由于您已经编辑了您的问题,下面是您编辑的问题的答案。
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;
}
所以我想声明一个函数 func(temp t1, temp t2) 并在 class temp 中使用它.但是我不想将它定义为 class temp 的成员函数,主要是因为我希望其他函数能够访问 func 不使用 temp 的任何对象。我知道可以通过将 func() 声明为 temp 的朋友来实现,但是有没有办法为 [=16 声明一种原型=]temp 以便我可以将它用作非成员函数的参数 temp 然后稍后定义它?
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 ...*/
};
我想在 class.
之外声明函数 rec_compIs there a way to declare a clss and then define it later in c++?
是的。只需省略 class 定义的大括号分隔主体即可获得非定义声明。这是 class:
的声明class temp;
declaring func() as a friend
将函数声明为 class 的友元仅在允许友元函数访问 class.
的受保护和私有成员和基类时有用So i want to declare a function func(temp t1, temp t2) and use it in class temp.
在声明函数之前声明 class(没有定义)确实可行。但这不是绝对必要的。同样有效的替代顺序:
- 定义
temp
- 声明
func
- 定义使用
func
的
temp
的成员函数
不过,我推荐先声明 class 的方法。按照声明 classes,声明函数,定义 classes,定义函数的模式通常最容易找到正确的顺序。
是的,这是可能的。举例如下:
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;
}
上面程序的输出可见here.
编辑
由于您已经编辑了您的问题,下面是您编辑的问题的答案。
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;
}