Using a class in a header file without access to its definition?
This is excerpt from google's c++ coding guidelines.
How can we use a class Foo in a header file without access to its definition?
- We can declare data members of type Foo* or Foo&.
- We can declare (but not define) functions with arguments, and/or return values, of type Foo. (One exception is if an argument Foo or const Foo& has a non-explicit, one-argument constructor, in which case we need the full definition to support automatic type conversion.)
- We can declare static data members of type Foo. This is because static data members are defined outside the class definition.
What I'm curious about is exception in the second bullet. Why is this so? Why is the fu开发者_如何学Pythonll definition needed if we want to support automatic type conversion?
My guess is that compiler needs the full definition of the destination type because of the temporary object that is created in the implicit conversion. Am I guessing correctly? Is there more to it?
EDIT:
As I see it, the exception in the guideline is addressed to situation like this:
class A
{
public:
A( int );
};
class B
{
public:
B( A const &a );
};
int main()
{
B b(2);
}
Here we have only one user-defined implicit conversion (from int to A), and call to constructor that accepts A const &. Only thing that makes sense in this exception is to support direct conversion from e.g. int to A, and then to B via constructor that accepts A const &, allowing client code to use this conversion chain without need to explicitly include header file where A class is declared.
The C++ language doesn't differentiate between code in header files and other file. It does not even require that a header is a file. So purely technically the question is meaningless, but in practice you restrict what you do in header files so as not to run afoul of the One Definition Rule. Without restricting yourself, users would have to be careful to only include the header file in one translation unit. With proper restrictions, the header file can be freely included in multiple translation units.
An incomplete type is one where the size is not known, where sizeof
cannot be used.
When the class definition is not known, class Foo
is necessarily incomplete.
This means you cannot do things that requires the size to be known. And since incompleteness means that members are not known (they would necessarily be known if the size was known) you can't generally call any members. Exception: you can call the destructor, like in delete pFoo
, and the compiler must accept that, but it's Undefined Behavior if class Foo
has a non-trivial destructor.
The exception noted in the Google guidelines is, however, meaningless.
EDIT: I discovered that people on SO like it better when things are spelled out in detail, so, adding discussion of why the guideline is meaningless.
The guideline says you can "declare (but not define)" but that "one exception is if an argument Foo or const Foo& has a non-explicit, one-argument constructor".
The declaration does not have anything to do with constructors, which one can affirm by simply trying it out:
#include <iostream>
struct Foo;
Foo bar( Foo const& ); // Declaration of function bar, works fine.
struct Foo
{
int x_;
Foo( int x ): x_( x ) {} // Converting constructor.
};
int main()
{
std::cout << bar( 42 ).x_ << std::endl;
}
Foo bar( Foo const& foo ) { return foo; }
In conclusion, again, the Google guidelines' exception is meaningless.
Cheers & hth.,
Suppose that foo.h
knows about Foo
declaration only
//foo.h
class Foo;
void f(const Foo &); // It is possible to use the reference.
Full definition is in foo.cpp
// foo.cpp
class CanBeConvertedToFoo;
class Foo
{
Foo (const CanBeConvertedToFoo & x); // implicit constructor
}
class CanBeConvertedToFoo
is implicit convertable to Foo;
But it is unknown in some.cpp
.
// some.cpp
#include "foo.h"
void g(const CanBeConvertedToFoo & x) {
f(x); // Is it known about implicit conversion ?
}
I don't know whether the exception in the second point is true. Implicit conversions must be know only when a function is called, not when it is declared, so the following works even though C
is incomplete while f
is declared:
#include <iostream>
class C;
void f(C);
struct C { C(int i) { std::cout << "C(" << i << ")" << std::endl; } };
void f(C c) { std::cout << "f(C)" << std::endl; }
int main() { f(2); }
精彩评论