Maestro
Maestro

Reputation: 2552

Can I forward-declare names -defined in other header in a header files?

I want to create a class Student that has a member of type library std::string but I don't want to include the header <string> in my Student.h and use only forward-declaration:

// Student.h
#ifndef STUDENT_H
#define STUDENT_H

#include <iostream>

typedef class string_ string;

struct Student
{
public:
    Student(const string_&, std::size_t);
    const string_ name()const;
    void setName(const string_&);
    std::size_t age()const;
    void setAge(std::size_t);
private:
    string_* name_ ;
    std::size_t age_;
};

 // Student.cpp
#include "Student.h"
#include <string>

Student::Student(const std::string& str, std::size_t a) :
    name_(&str),
    age_(a)
{}

"The syntax typedef struct _FILETIME FILETIME is a kind of forward declaration for FILENAME syntax. As we only use a reference, we can avoid including the tag in our file SysInfoWindowsImpl.h and keep it in the CPP file." from the book.

Upvotes: 0

Views: 307

Answers (2)

No, you cannot today (in may 2020). See also this and n3337 (the C++11 standard).

But C++20 or later might add modules, and then things become different.

Practically speaking, for a small program (e.g. less than a few dozen thousands lines of your C++ code) a common approach is to have a single header file -which you would #include in each of your translation units, and that common master header file would #include many standard headers. With recent GCC, you could use precompiled headers (and practically speaking, that works the best with a single master header, perhaps #include -ing sub header files; see this explanation)

Consider also using a good enough build automation toolset. On Linux, that might be a combination of GCC or Clang, ccache, with make or ninja, etc...

Upvotes: -1

Asteroids With Wings
Asteroids With Wings

Reputation: 17454

Yes, but only if they match.

You forward declared a global type string, not in namespace std.

You could probably make it work with namespace std {} but then your program would have undefined behaviour, because you're not allowed to declare new things in that namespace (with a few exceptions).

In general, you want to avoid forward declarations for anything but your own classes.

Just #include <string>. If doing so is causing problems, you should resolve those independently.

Upvotes: 3

Related Questions