热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在C++中转发“预先声明”一个类-Forward“Pre-declaring”aClassinC++

IhaveasituaioninwhichIwanttodeclareaclassmemberfunctionreturningatypethatdependso

I have a situaion in which I want to declare a class member function returning a type that depends on the class itself. Let me give you an example:

我有一个情境,我想声明一个类成员函数返回一个依赖于类本身的类型。让我给你举个例子:

class Substring {
    private:
        string the_substring_;
    public:
        // (...)
        static SubstringTree getAllSubstring(string main_string, int min_size);
};

And SubstringTree is defined as follows:

而SubstringTree的定义如下:

typedef set SubstringTree;

My problem is that if I put the SubstringTree definition after the Substring definition, the static method says it doesn't know SubstringTree. If I reverse the declarations, then the typedef says it doesn't know Substring.

我的问题是如果我在Substring定义之后放置SubstringTree定义,静态方法说它不知道SubstringTree。如果我反转声明,那么typedef表示它不知道Substring。

How can I do it? Thanks in advance.

我该怎么做?提前致谢。

4 个解决方案

#1


You could define it inside the class:

你可以在类中定义它:

class Substring {
    private:
        string the_substring_;
    public:
        // (...)
        typedef set SubstringTree;
        static SubstringTree getAllSubstring(string main_string, int min_size);
};

#2


As you've written it, the short answer is you can't.

正如你所写,简短的回答是你不能。

You do have a few close alternatives:

你有几个接近的选择:

1) Declare SubstringTree in Substring

1)在子串中声明SubstringTree

class Substring {
public:
    class Comparator;
    typedef set Tree;

private:
    string the_substring_;
public:
    // (...)
    static Tree getAllSubstring(string main_string, int min_size);
};

typedef Substring::Tree SubstringTree;

2) Define the Comparator outside of Substring:

2)在子串外定义比较器:

class Substring;
class SubstringComparator;
typedef set SubstringTree;

class Substring {
public:

private:
    string the_substring_;
public:
    // (...)
    static SubstringTree getAllSubstring(string main_string, int min_size);
};

3) You can use a template to delay the lookup until you have more declarations:

3)您可以使用模板来延迟查找,直到您有更多声明:

template 
struct TreeHelper
{
  typedef set Tree;
};

class Substring {
public:
  class Comparator;

private:
  string the_substring_;
public:
  // (...)
  static TreeHelper::Tree getAllSubstring(string main_string
                                             , int min_size);
};

typedef TreeHelper::Tree SubstringTree;

#3


You can predeclare a class with this:

你可以用这个预先声明一个类:

class Foo;

Keep in mind that before the class is actually defined, you can only declare pointers to it, not instances.

请记住,在实际定义类之前,您只能声明指向它的指针,而不是实例。

#4


forward declaration

class Substring;

I don't know if that will work for non pointer uses of Substring though.

我不知道这是否适用于Substring的非指针使用。


推荐阅读
  • 阿里Treebased Deep Match(TDM) 学习笔记及技术发展回顾
    本文介绍了阿里Treebased Deep Match(TDM)的学习笔记,同时回顾了工业界技术发展的几代演进。从基于统计的启发式规则方法到基于内积模型的向量检索方法,再到引入复杂深度学习模型的下一代匹配技术。文章详细解释了基于统计的启发式规则方法和基于内积模型的向量检索方法的原理和应用,并介绍了TDM的背景和优势。最后,文章提到了向量距离和基于向量聚类的索引结构对于加速匹配效率的作用。本文对于理解TDM的学习过程和了解匹配技术的发展具有重要意义。 ... [详细]
  • Google Play推出全新的应用内评价API,帮助开发者获取更多优质用户反馈。用户每天在Google Play上发表数百万条评论,这有助于开发者了解用户喜好和改进需求。开发者可以选择在适当的时间请求用户撰写评论,以获得全面而有用的反馈。全新应用内评价功能让用户无需返回应用详情页面即可发表评论,提升用户体验。 ... [详细]
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • 如何用JNI技术调用Java接口以及提高Java性能的详解
    本文介绍了如何使用JNI技术调用Java接口,并详细解析了如何通过JNI技术提高Java的性能。同时还讨论了JNI调用Java的private方法、Java开发中使用JNI技术的情况以及使用Java的JNI技术调用C++时的运行效率问题。文章还介绍了JNIEnv类型的使用方法,包括创建Java对象、调用Java对象的方法、获取Java对象的属性等操作。 ... [详细]
  • 本文整理了Java面试中常见的问题及相关概念的解析,包括HashMap中为什么重写equals还要重写hashcode、map的分类和常见情况、final关键字的用法、Synchronized和lock的区别、volatile的介绍、Syncronized锁的作用、构造函数和构造函数重载的概念、方法覆盖和方法重载的区别、反射获取和设置对象私有字段的值的方法、通过反射创建对象的方式以及内部类的详解。 ... [详细]
  • 在C#中,使用关键字abstract来定义抽象类和抽象方法。抽象类是一种不能被实例化的类,它只提供部分实现,但可以被其他类继承并创建实例。抽象类可以用于类、方法、属性、索引器和事件。在一个类声明中使用abstract表示该类倾向于作为其他类的基类成员被标识为抽象,或者被包含在一个抽象类中,必须由其派生类实现。本文介绍了C#中抽象类和抽象方法的基础知识,并提供了一个示例代码。 ... [详细]
  • 1Lock与ReadWriteLock1.1LockpublicinterfaceLock{voidlock();voidlockInterruptibl ... [详细]
  • PeopleSoft安装镜像版本及导入语言包的方法
    本文介绍了PeopleSoft安装镜像的两个版本,分别是VirtualBox虚拟机版本和NativeOS版本,并详细说明了导入语言包的方法。对于Windows版本,可以通过psdmt.exe登录进入,并使用datamover脚本导入语言包。对于Linux版本,同样可以使用命令行方式执行datamover脚本导入语言包。导入语言包后,可以实现多种语言的登录。参考文献提供了相关链接以供深入了解。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 如何使用Python从工程图图像中提取底部的方法?
    本文介绍了使用Python从工程图图像中提取底部的方法。首先将输入图片转换为灰度图像,并进行高斯模糊和阈值处理。然后通过填充潜在的轮廓以及使用轮廓逼近和矩形核进行过滤,去除非矩形轮廓。最后通过查找轮廓并使用轮廓近似、宽高比和轮廓区域进行过滤,隔离所需的底部轮廓,并使用Numpy切片提取底部模板部分。 ... [详细]
  • HashMap的扩容知识详解
    本文详细介绍了HashMap的扩容知识,包括扩容的概述、扩容条件以及1.7版本中的扩容方法。通过学习本文,读者可以全面了解HashMap的扩容机制,提升对HashMap的理解和应用能力。 ... [详细]
  • 在IDEA中运行CAS服务器的配置方法
    本文介绍了在IDEA中运行CAS服务器的配置方法,包括下载CAS模板Overlay Template、解压并添加项目、配置tomcat、运行CAS服务器等步骤。通过本文的指导,读者可以轻松在IDEA中进行CAS服务器的运行和配置。 ... [详细]
author-avatar
手机用户2502898783
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有