Using forward declarations for build in datatypes.
Posted
by bdhar
on Stack Overflow
See other posts from Stack Overflow
or by bdhar
Published on 2010-06-07T11:46:59Z
Indexed on
2010/06/07
11:52 UTC
Read the original article
Hit count: 293
c++
|forward-declaration
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.
© Stack Overflow or respective owner