使用内置数据类型的向前声明 [英] Using forward declarations for built in datatypes

查看:121
本文介绍了使用内置数据类型的向前声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个类 Person

code>这样。

  #pragma once 

#include< string&

class Person
{
public:
Person(std :: string name,int age);
std :: string GetName(void)const;
int GetAge(void)const;
private:
std :: string _name;
int _age;
};

和一个类 Student p>

  #pragma once 

#include< string>

class Person;

class Student
{
public:
Student(std :: string name,int age,int level = 0);
Student(const Person& person);
std :: string GetName(void)const;
int GetAge(void)const;
int GetLevel(void)const;
private:
std :: string _name;
int _age;
int _level;
};

在Student.h中,我有一个前进声明 class Person; code>在我的转换构造函数中使用 Person 。精细。但是我在代码中使用 std :: string 时,已经做了 #include< string> 如何使用forward声明这里避免编译错误?是否有可能?



感谢。

解决方案

c $ c> string as

  std :: string _name; 
// ^^^^^^^^^具体成员

整个结构 string 将需要,所以必须声明。您必须 #include< string>






string 可以省略,例如

  std :: string * _name; 
// ^^^^^^^^^^指针或参考

可以使用向前声明,但我仍然建议您这样做,因为 std :: string 不是一个简单的结构类型像Person或学生,但涉及许多模板的非常复杂的类型:

  template< class charT,class traits = char_traits< charT& = allocator< charT> > 
class basic_string {...};
typedef basic_string< char>串;

如果你向前声明错误(例如 class string; ),编译将失败,当你实际使用它,因为冲突的类型。


I understand that wherever possible we shall use forward declarations instead of includes to speed up the compilation.

I have a class Person like this.

#pragma once

#include <string>

class Person
{
public:
    Person(std::string name, int age);
    std::string GetName(void) const;
    int GetAge(void) const;
private:
    std::string _name;
    int _age;
};

and a class Student like this

#pragma once

#include <string>

class Person;

class Student
{
public:
    Student(std::string name, int age, int level = 0);
    Student(const Person& person);
    std::string GetName(void) const;
    int GetAge(void) const;
    int GetLevel(void) const;
private:
    std::string _name;
    int _age;
    int _level;
};

In Student.h, I have a forward declaration class Person; to use Person in my conversion constructor. Fine. But I have done #include <string> to avoid compilation error while using std::string in the code. How to use forward declaration here to avoid the compilation error? Is it possible?

Thanks.

解决方案

Since used string as

std::string _name;
//^^^^^^^^^ concrete member    

the whole structure of string would be needed, so the declaration must be needed. You must #include <string>.


Declaration of string can be omitted possible if you write, e.g.

std::string* _name;
//^^^^^^^^^^ pointer or reference

which you could use a forward declaration, but I still recommend you not to do so, because std::string is not a simple structure type like Person or Student, but a very complex type involving many templates:

template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
class basic_string { ... };
typedef basic_string<char> string;

If you forward declare it wrongly (e.g. class string;), the compilation will fail when you actually use it because of conflicting type.

这篇关于使用内置数据类型的向前声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆