ThaHypnotoad ThaHypnotoad - 1 year ago 56
C++ Question

c++: Any way to avoid typing the class name before every member function?

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:

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download