2

I started out programming 5 years ago in java, so when I moved over to c++ 2 years ago, implementations of member functions were rather irritating.

Foo::bar(){/*some stuff*/}
Foo::baz(){/*some other stuff*/}

Back then I just kinda got used to it, but recently I wondered if there was any way to structure code to avoid typing that Foo:: every function, perhaps something like:

Foo::{
    bar(){//some stuff}
    baz(){//some other stuff}
}

I've found that even after 2 years I still have trouble reading even my own code because the name of the function itself isn't the first thing in the line.

edit: Since this question is a duplicate, I thought I would share one thing I found clicking on links. This is definitively not possible at the moment, but there is a proposal to add it to the standard. Don't know if or when it might actually get added, but if you're reading this a few years from now this could be a good lead.

ThaHypnotoad
  • 165
  • 2
  • 5
  • 12

1 Answers1

1

No, there is not (unless you define a member function contextually with its declaration, thus when you define your class - that is more or less what happens in Java actually).

In other terms, if you don't want to do this:

// .h
struct S { void f(); };
// .cpp
void S::f() {}

You can still do this:

// .h
struct S { void f() {} };

Anyway it has drawbacks and you could not be prepared to deal with them in any situation.

skypjack
  • 49,335
  • 19
  • 95
  • 187
  • Problem is that your examples are not completely equal. – Logman Nov 12 '16 at 23:35
  • @Logman: The second increases likelihood that calls to `S::f()` can be inlined, but the two are functionally equivalent. – Peter Nov 13 '16 at 00:55
  • I would also like to be able to keep the class declaration and the implementation separate. I take it that I can't have my cake and eat it in this case? – ThaHypnotoad Nov 13 '16 at 01:01