Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a reference to a forward-declared type (C++)

I have a class method that returns a reference to something. When I use this method, if I only have a forward declaration of that something, I can only compile if I assign the output of the method. I really don't understand why...

Here's a simplified example:

ClassA.h:

//Forward declare
class ClassB;

class ClassA
{
public:
    ClassA(void);
    ~ClassA(void);

    ClassB& Func();
};

ClassA.cpp:

#include "ClassA.h"

#include "ClassB.h"

ClassA::ClassA(void)
{}

ClassA::~ClassA(void)
{}

static ClassB c;

ClassB& ClassA::Func()
{
    return c;
}

ClassB.h:

#pragma once
class ClassB
{
public:
    ClassB(void) {};
    ~ClassB(void) {};
};

Now, if I call ClassA::Func without assigning the return value (while only having a forward declaration of ClassB), it will not compile:

main.cpp:

#include "ClassA.h"

int main(void)
{
    ClassA a;

    a.Func(); //error C2027: use of undefined type 'ClassB'

    return 0;
}

If I use this line instead, it works: ClassB& b = a.Func();

What's happening here? Why would the compiler need to know the size of ClassB or what its methods are when the return value is not assigned anywhere?

I'm compiling this with VisualStudio 2010 SP1.

like image 494
Samuel Delisle Avatar asked Aug 27 '14 17:08

Samuel Delisle


People also ask

What is a forward reference C?

Forward reference is when you declare a type but do not define it. It allows you to use the type by pointer (or reference for C++) but you cannot declare a variable. This is a way to say to the compiler that something exists. Say that you have a Plop structure defined in Plop.

How do you fix a forward declaration in C++?

In C++, Forward declarations are usually used for Classes. In this, the class is pre-defined before its use so that it can be called and used by other classes that are defined before this. Example: // Forward Declaration class A class A; // Definition of class A class A{ // Body };

Does C require forward declaration?

Classes. In some object-oriented languages like C++ and Objective-C, it is sometimes necessary to forward-declare classes. This is done in situations when it is necessary to know that the name of the class is a type, but where it is unnecessary to know the structure.

Do C++ need forward declarations?

C++ Programming Forward declarations means the declaration of a method or variable prior to its implementation. Such declaration is necessary in C/C++ programming language in order to be able to use a variable or object before its implementation.


1 Answers

Looks like a "limitation" of the compiler, the MSDN page for C2027 says :

It is possible to declare a pointer to a declared but undefined type. But Visual C++ does not allow a reference to an undefined type.

The following sample generates C2027.

And gives this example :

class A;
A& CreateA();

class B;
B* CreateB();

int main() {
   CreateA();   // C2027
   CreateB();   // OK
}

So both of your examples are supposed to generate a C2027, I'm not sure why the second does not (that is, without more documentation, it is a bug)

like image 148
quantdev Avatar answered Oct 06 '22 01:10

quantdev