#Class definition vs class declaration

50 messages · Page 1 of 1 (latest)

soft breach
#

I’m confused on this terminology. I understand function definition vs declaration but it seems like a class declaration is a definition so what is the difference? Is a class declaration just the class without any method bodies?

tropic hullBOT
#

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.

turbid jungle
#

;compile

class C;
int main(){}
summer falconBOT
#
Compilation successful
turbid jungle
#

there is declaration but no definition

#

Is a class declaration just the class without any method bodies?
so no

soft breach
#

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

gleaming temple
gleaming temple
#

and yes member declarations are also declarations

neon horizon
soft breach
#

Ie. Friend functions

soft breach
#

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

gleaming temple
soft breach
#

I put a friend function inside a class definition and define the function else where and it worked fine

gleaming temple
gleaming temple
soft breach
#

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

gleaming temple
soft breach
#

Im confused on what’s going on

soft breach
gleaming temple
gleaming temple
#

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

gleaming temple
soft breach
gleaming temple
#

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();
}
summer falconBOT
#
Compiler Output
<source>: In function 'int main()':
<source>:8:3: error: 'fun' was not declared in this scope
    8 |   fun();
      |   ^~~
Build failed
gleaming temple
#

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);
}
summer falconBOT
#
Compiler Output
<source>: In function 'int main()':
<source>:14:3: error: 'fun' was not declared in this scope
   14 |   fun(0);
      |   ^~~
Build failed
gleaming temple