I have the following code
class foo
{
public:
foo() {}
private:
int foo_int;
friend class bar; //----->Statement A
};
class bar
{
public:
void someMethod()
{
foo f;
f.foo_int = 13;
}
};
Now I also read this answer on SO. However I cant put the pieces of puzzle together as to why
the compiler recognizes bar as a type. I was under the impression that it would complain of Bar being an incomplete type however that did not happen. My question is why ?
friend class bar;
is simply a declaration ... there's nothing for the compiler to complain about. The limitation on incomplete types is when the compiler needs information about the type that it doesn't have, such as its size or, for base classes, its members, but for friend it doesn't need anything other than its name.
Note that it doesn't matter where the friend declaration occurs in the class definition ... that it follows private: doesn't make it private. It's better to put it at the top of the class definition.
A friend specification of a class that has not been declared yet acts as a declaration of the class. It is perfectly fine to declare an incomplete type as a friend of a class because the compiler only needs to know about the type being declared.
Related
class B;
class A {
private:
int numA;
public:
A(): numA(12) { }
// friend function declaration
friend int add(A, B);
};
this does not give any error on declaring object of class B in friend
function,,but this gives,,as firstly class B is declared
class Apple;
class B {
private:
int b;
public:
void showA(Apple d)
{
// Since B is friend of A, it can access
// private members of A
cout << "A::a=" ;
}
};
};
this gives an error of incomplete type for object d,,why this is happening though we already declared class apple before,
Why does the first example compile?
In your first example, you have a forward declaration of class B followed by a declaration of a friend function that uses it in its parameter list:
class B;
class A {
...
friend int add(A, B);
};
This is allowed because, although B is incomplete, we are not defining add yet, only declaring an intention to eventually do so.
Why does the second example not compile?
In the second example, we have a forward declaration of class Apple, followed by a definition of showA:
class Apple;
class B {
...
void showA(Apple d)
{
...
}
};
This time, since we are defining the function, the compiler is obligated to generate code for it. But because Apple is incomplete, the compiler cannot know, for example, how much space in memory to reserve to hold the parameter d. Therefore this is an error.
The question When can I use a forward declaration? explains some of what can and cannot be done with an incomplete (forward-declared) type.
The use of 'friend' is irrelevant here
The friend keyword is basically irrelevant here. friend primarily affects access control (in the sense of public and private), but that's not the issue here.
A detail: friend also affects scoping. Because of friend, add is not a member of class A, but rather refers to a member of the global scope without actually introducing one (it's weird). But that does not change whether an incomplete type can be used as a parameter.
I'm getting this strange problem which I don't know why happens. The first and second of the following code snippets compile, while the third does not:
Compiles:
class Foo {
public:
Foo() { Bar(); }
private:
class Bar {};
};
Compiles:
class Foo {
class Bar {}; // Or only forward declare here and define later
public:
Foo(Bar) {}
}
Does not compile:
class Foo {
public:
Foo(Bar) {}
private:
class Bar {};
};
What makes the third fail to compile while the first can?
Normally, in C++, you can only reference declarations that were previously made in the translation unit. However, within a class definition, the definition of member functions are allowed to reference declarations which are made later in the class. Basically, the compiler restructures your in-class definitions so that they work as though they were written just after the class.
But this is only true of the function definitions. The declaration of the function (including parameter types) isn't allowed to do this. They can only reference declarations that have already been made in file order.
So you can do this:
class Test
{
public:
void Func(int x) {Inner foo;}
private:
class Inner {};
};
But not this:
class Test
{
public:
void Func(Inner x) {}
private:
class Inner {};
};
First example does not expose anything about private Bar to the outside, while third does.
Third example is pretty much saying, that there exist some class Foo, which has constructor with single argument of type Bar. But Bar is unknown to the outside. Imagine calling such constructor.
Foo f{Foo::Bar{}};
Will result probably in something like Foo::Bar is inaccessible.
The more I program, the more I like the snake_case and the less the CamelCase. There is one particular inconvenience irritating me a bit: I do not like getters start from the get_ prefix, but that is not always possible without some extra work when using the snake_case. Consider, for instance, a getter for a member of enum type defined inside a class,
class bar
{
public:
enum class foo { a, b };
foo foo() const { return m_foo; }
void set_foo(foo new_foo) { m_foo = new_foo; }
private:
foo m_foo;
};
This won't compile since (if I understand correctly) enum foo is hidden in both the setter new_foo parameter type and the private m_foo member declaration by the prior foo() getter declaration. The obvious fix is to name the getter as get_foo, but as I said, I don't like it.
When I compile with Clang it suggests another "workaround":
error: must use 'enum' tag to refer to type 'foo' in this scope
void set_foo(foo new_foo) { m_foo = new_foo; }
^
enum
and the same for m_foo member. The following code indeed compiles fine
class bar
{
public:
enum class foo { a, b };
foo foo() const { return m_foo; }
void set_foo(enum foo new_foo) { m_foo = new_foo; }
private:
enum foo m_foo;
};
It looks like the elaborated type specifiers are even supposed to solve such kind of problems:
Elaborated type specifiers may be used to refer to a
previously-declared class name (class, struct, or union) or to a
previously-declared enum name even if the name was hidden by a
non-type declaration.
But is not that a misuse of a language feature? Have you ever seen anything like that in libraries that use snake_case, such as, for instance, the Standard Library or Boost, or write it yourself?
I've just recently learned about friend class concept in C++ (I've googled around for a bit, but this answer made me laugh until I remembered the most important parts), and I'm trying to incorporate it in to the project I'm working on right now. The concise question is singled out in the end, but in general, I'm confused by complete lack of forward declarations in my working code.
All of my classes are separated through (sub-)folders and each one into a separate .h and .cpp file, but this should be enough to get a feeling about dependencies:
// FE.h - no implementations - no .cpp file
class FE
{
private:
virtual void somePrivateFunc() = 0;
// 90% virtual class, interface for further implementations
friend class TLS;
};
// DummyFE.h
#include "FE.h"
class DummyFE :: public FE {
/* singleton dummy */
private:
// constructor
public:
static DummyFE& instance();
};
// DummyFE.cpp
#include "DummyFE.h"
// all Dummy FE implementation
// ImplFE.h
#include "FE.h"
class ImplFE :: public FE { /* implemented */ };
// ImplFE.cpp
#include "FE.cpp"
// all Impl FE implementations
// SD.h - implements strategy design pattern
// (real project has more than just FE class in here)
#include "FE.h"
#include "DummyFE.h"
class SD
{
private:
FE &localFE;
public:
SD(FE ¶mFE = DummyFE::instance());
// ... and all the other phun stuff ...
friend class TLS;
};
// SD.cpp - implementations
# include "SD.h"
/* SD implemented */
// TLS.h - implements strategy design pattern
(on a higher level)
#include SD.h
class TLS{
private:
SD *subStrategy;
public:
void someFunctionRequiringFriendliness();
}
// TLS.cpp - implementations
#include "TLS.h"
void TLS::someFunctionRequiringFriendliness(){
this->subStrategy->localFE.somePrivateFunc(); // ok!
}
Now, I've had party getting all of this to actually compile with all the dependencies (had to write it down in to a class diagram in the end to make it work), but now it does. The fact that is actually confusing me, is that no forward declarations were needed. I know about forward declarations from before, and just in case, I refreshed my memory with this answer.
So, to try and keep it clear, my question:
When declaring the class TLS as a friend, how come no explicit forward declarations were needed? Does that mean that a friend class declaration is a forward declaration all in it self? For me, intuitively, something here is missing... And since it compiles and works normally, can somebody help correct my intuition? :D
PS sorry for such a lengthy introduction to the question and a bunch of code. Please, don't comment on my code concept - friends are good here, I'm pretty sure it's correct for my current project (it's just a bit hard to see from this skeleton). I'd just like to know why no forward declaration was needed anywhere.
You're right, the friend declaration is kind of like a forward declaration.
The following compiles:
class A;
class B
{
friend A;
};
or
class B
{
friend class A;
};
this does not:
class B
{
friend A;
};
It's not actually the friend declaration that forward-declares class A, but the class keyword. That's why the second example doesn't work, because it doesn't know what A is. If you declare A beforehand, like in the first snippet, it can resolve A to a class declaration.
I stand corrected.
friend class TLS;
This syntax is a declaration in itself, that is why you don't need an extra previous declaration of the type. Note that the friend declaration is slightly different (specially for functions) than a declaration in the enclosing namespace.
In particular, unless there is also a declaration in the enclosing namespace, a function declared in a friend declaration can only be found by argument dependent lookup (and cannot be defined outside of the class). The same goes for classes, unless there is also a declaration at namespace level, the type so declared will not be available outside of the class declaring it as a friend.
class B {
friend class A;
};
//A foo(); // Error: A is not declared here!
class A;
A foo(); // Fine
Does that mean that a friend class declaration is a forward
declaration all in it self?
Yes
forward declaration does not need to be at the top of the file as follows
class A;
class C;
class D;
class B
{
A* a;
C* c;
D* d;
};
is the same as
class B
{
class A* a;
class C* c;
class D* d;
};
the recommended friend syntax just uses the later
Sometimes, C++'s notion of privacy just baffles me :-)
class Foo
{
struct Bar;
Bar* p;
public:
Bar* operator->() const
{
return p;
}
};
struct Foo::Bar
{
void baz()
{
std::cout << "inside baz\n";
}
};
int main()
{
Foo::Bar b; // error: 'struct Foo::Bar' is private within this context
Foo f;
f->baz(); // fine
}
Since Foo::Bar is private, I cannot declare b in main. Yet I can call methods from Foo::Bar just fine. Why the hell is this allowed? Was that an accident or by design?
Oh wait, it gets better:
Foo f;
auto x = f.operator->(); // :-)
x->baz();
Even though I am not allowed to name the type Foo::Bar, it works just fine with auto...
Noah wrote:
type names defined within a class definition cannot be used outside their class without qualification.
Just for fun, here is how you can get at the type from outside:
#include <type_traits>
const Foo some_foo();
typedef typename std::remove_pointer<decltype( some_foo().operator->() )>::type Foo_Bar;
Trying to find anything in the standard that would spell it out in detail but I can't. The only thing I can find is 9.9:
Type names obey exactly the same scope rules as other names. In particular, type names defined within a class definition cannot be used outside their class without qualification.
Essentially, the name of Foo::Bar is private to Foo, not the definition. Thus you can use Bars outside of Foo, you just can't refer to them by type since that name is private.
The name lookup rules for members would also seem to have some effect on this. I don't see anything that specifically references "nested class" and thus they wouldn't be allowed to (if my lack of finding anything in fact is because it's not there).
I can't provide a full answer, but maybe a starting point. The C++ 1998 specification includes the following code example under paragraph 11.3 [class.access] (p. 175):
class A
{
class B { };
public:
typedef B BB;
};
void f()
{
A::BB x; // OK, typedef name A::BB is public
A::B y; // access error, A::B is private
}
In this example, a private type is "published" through a public typedef. Although it's not the same thing as publishing a type through a member function signature, it's similar.
I think this is by design. You cannot explicitly create instance of Foo::Bar but it could be returned from member functions and then you could pass it to other member functions. This lets you to hide implementation details of your class.