Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative implementation syntaxes for class members on c++

Tags:

c++

When declaring and implementing a class or struct in c++ we generally do:

H file

namespace Space{
  class Something{
    void method();
  }
}

CPP file

void Space::Something::method(){
  //do stuff
}

OR

namespace Space{
  void Something::method(){
    //do stuff
  }
}

Note how it is possible to wrap all the implementations inside the namespace block so we don't need to write Space:: before each member. Is there a way to wrap class members in some similar way?

Please note I want to keep source and header file separated. That's generally a good practice.

like image 952
ButterDog Avatar asked Mar 22 '23 23:03

ButterDog


2 Answers

Not without sacrificing the separation between header and implementation (cpp) file.

You can declare everything inline in the header inside the class, but it's very messy for large classes.

What's the problem you're actually trying to solve? Or is it just typing time? :)

like image 122
SteveLove Avatar answered Apr 26 '23 22:04

SteveLove


Yes:

.h file:

namespace Space{
  class Something{
    void method()
    {
      // do stuff
    }
  };
}

I don't recommend it, though.

like image 31
Bill Avatar answered Apr 26 '23 23:04

Bill