#Class definition vs class declaration
50 messages · Page 1 of 1 (latest)
When your question is answered use !solved or the button below to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
;compile
class C;
int main(){}
there is declaration but no definition
Is a class declaration just the class without any method bodies?
so no
Okay so it’s just a forward declaration then
One more thing
So in classes when you have methods or friend functions you don’t need to forward declare them outside the class before implementing their bodies else where. So does this mean that the declaration is inside the class
this terminology isn't exactly used consistently by different sources/people
if you come across a source that uses specifically declaration and definition, then yes a declaration would be a "forward declaration", just that "forward declaration" isn't really the standard term iirc, it's just widespread and I guess some people like it better and find it less ambiguous/overloaded
a friend declaration is a declaration
and yes member declarations are also declarations
Class declarations just Informs the compiler it exists
So if you put it in a class you don’t need another declaration which I’ve seen. So I should just know this works as if it’s already declared outside the class?
Ie. Friend functions
It's complicated
So it would be useless to know why it works? Just know that it works? Lol
I was just confused at first when doing friend functions because I realize putting it inside the class as a friend function can double as a declaration
I don't understand how you reached that conclusion
Wdym? I tried it out
I put a friend function inside a class definition and define the function else where and it worked fine
I don't even fully understand that sentence, but whether or not you need a different declaration than the friend declaration depends on how you intend to use the function in the first place
This is a reply to a message where I say
"I don't understand how you reached the conclusion that it's useless to know why it works"
If I have a header with MyClass Definrion and inside the MyClass defitniom I put friend void foo();
I can define foo in a separate file fine. I wouldn’t need to also do void foo() in the header
Implied there's no relation to "trying things out", since this is all about knowledge being useful or useless
Im confused on what’s going on
I was just asking why this works how it does and you said it’s complicated and I asked if whether or not It’s just good enough to know that it works then (if it’s complicated) or should I know why it works like this.
You said "it's useless to know why it works", I've asked why you would say that
On a different topic, there's the whole "when or why would you declare a function" which I haven't addressed since the moment I asked about why you think it's useless to know stuff
Part of the reason I said "it's complicated" is because having just the friend declaration for a function may lead to surprising results as to what function exactly is being declared
In the first place, a declaration for a function just informs that this name/identifier names a function with a specific type
The issue is then generally about what the exact name is, and how it can be reached/seen
This can lead to somewhat surprising results when you have namespaces
Specifically there are restrictions if the class is in some namespace but the function should be in another namespace
This is how cppreference puts it
A name first declared in a friend declaration within a class or class template X becomes a member of the innermost enclosing namespace of X, but is not visible for lookup (except argument-dependent lookup that considers X) unless a matching declaration at namespace scope is provided
Anyway the general point I'm trying to make, is that having "only" a friend declaration is not strictly equivalent to having a non-friend declaration
Also this isn't clear to me what you've tried to test
There has never been a need to declare a function before defining it, so I assume you did something else in your test, to actually test something
Misunderstanding then, I asked a question I didn’t state that it was lol
But then there is the question of how you dealt with function name lookup, unless you were in a situation where ADL applied
And this is where it really gets "complicated"
For a beginner
Arguably if you want to really understand and get proficient with c++ there is a point where you'll need to look at ADL, imo
Or at least remember that "operator overloads" and other "common operation customisation point" should be members or friends of the type
Right, and if you generic code, the topic of ADL also comes up when you need to consider options to let your user provide custom behaviours
;compile
struct Foo
{
friend void fun();
};
int main()
{
fun();
}
<source>: In function 'int main()':
<source>:8:3: error: 'fun' was not declared in this scope
8 | fun();
| ^~~
Build failed
One consequence of that, is that the above does not work
So even if a function should be a friend to get access to whatever private member of a class, you should consider "who" needs to call the function
;compile
struct Foo
{
operator int() const { return 0; }
friend void fun(int value) {}
};
int main()
{
// ok
fun(Foo{});
// not ok
fun(0);
}
<source>: In function 'int main()':
<source>:14:3: error: 'fun' was not declared in this scope
14 | fun(0);
| ^~~
Build failed
Random example with ADL, which is where it gets "weird"
Oh I love ADL just for that