【问题标题】:Is there a way to declare a class and then define it later in c++?有没有办法声明一个类,然后在 C++ 中定义它?
【发布时间】:2022-01-13 12:58:04
【问题描述】:

所以我想声明一个函数 func(temp t1, temp t2) 并在 class temp 中使用它。但我不想将它定义为 class temp 的成员函数,主要是因为我希望其他函数能够访问 func 而无需使用 的任何对象温度。我知道可以通过将 func() 声明为 temp 的朋友,但是有没有办法为 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 ...*/
};

我想在类外声明函数rec_comp

【问题讨论】:

  • 你的意思是像class Foo;吗?但它只适用于引用/指针。
  • 那你有一个糟糕的实现设计。
  • 是的。允许这样的事情吗?
  • @GauravShukla 然后请创建一个带有所需用例的小示例。但我也闻到了一些糟糕的设计
  • 声明在类内作为友元函数,然后在类外define(实现)?无论如何,如果没有完整的类定义,您将无法调用该函数,因此不妨在类中将其声明为 friend 函数。

标签: c++ function class oop


【解决方案1】:

有没有办法声明一个 clss,然后在 c++ 中定义它?

是的。只需省略类定义的大括号分隔主体即可获得非定义声明。这是一个类的声明:

class temp;

将 func() 声明为好友

将函数声明为类的友元仅在允许友元函数访问类的受保护和私有成员和基时有用。

所以我想声明一个函数 func(temp t1, temp t2) 并在 temp 类中使用它。

在声明函数之前声明类(没有定义)确实可行。但这不是绝对必要的。也适用的替代顺序:

  • 定义temp
  • 声明func
  • 定义使用functemp的成员函数

但是,我确实推荐先声明类的方法。按照声明类、声明函数、定义类、定义函数的模式,通常最容易找到正确的顺序。

【讨论】:

    【解决方案2】:

    是的,这是可能的。下面给出一个例子:

    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;
    }
    

    ma​​in.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;
    }
    

    【讨论】:

    • @GauravShukla 您可以将rec_comp 设为friendrecord,如我上面的答案所示。我已经编辑了我的答案以回答您编辑的问题。看看吧。
    • 请记住,使用这种方法,仅包含func.h 并不能实现太多目标。特别是如果需要定义类Temp的成员函数,创建Temp的实例,或者调用函数func()是不够的。在所有这些情况下,还需要包含temp.h。例如,从main.cpp 中删除#include "temp.h"main.cpp 将无法编译。
    • @Peter 当然,我不能讨论每一种可能性。我刚刚根据原始问题中的描述给出了一个可能的示例,以便 OP 可以将其作为参考(起点)。
    猜你喜欢
    • 2016-05-08
    • 2012-05-14
    • 2017-11-03
    • 1970-01-01
    • 2018-02-23
    • 2015-11-05
    • 2011-02-09
    • 1970-01-01
    • 2012-05-21
    相关资源
    最近更新 更多