Is it possible to avoid repeating the class name in the implementation file?

后端 未结 8 1975
迷失自我
迷失自我 2020-11-27 21:39

Is there a way to avoid the Graph:: repetition in the implementation file, yet still split the class into header + implementation? Such as in:

Header Fi

相关标签:
8条回答
  • 2020-11-27 21:51

    No, there is no way to avoid it. Otherwise, how would you know if a given function definition is for a class function or for a static function?

    0 讨论(0)
  • 2020-11-27 21:51

    One option is using. If you have method definitions which are in a cpp file that never gets #included, then using is safe (doesn't affect other files):

    foo.h:

    class FooLongNameSpecialisationsParamaters
    {
        int x_;
    
    public:
    
        int Get () const;
        void Set (int);
    };
    

    foo.cpp:

    #include "foo.h"
    
    using Foo = FooLongNameSpecialisationsParamaters;
    
    int Foo::Get () const
    {
        return x_;
    }
    
    void Foo::Set (int x)
    {
        x_ = x;
    }
    

    main.cpp:

    #include "foo.h"
    
    int main ()
    {
        //Foo foo; <-- error
        FooLongNameSpecialisationsParamaters foo;
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-27 21:55

    If you are asking if you can define a member function such as Graph::printGraph without specifying the class name qualification, then the answer is no, not the way that you want. This is not possible in C++:

    implementation file:

    void printEdge(){};
    

    The above will compile just fine, but it won't do what you want. It won't define the member function by the same name within the Graph class. Rather, it will declare and define a new free function called printEdge.

    This is good and proper, if by your point of view a bit of a pain, because you just might want two functions with the same name but in different scopes. Consider:

    // Header File
    class A
    {
      void foo();
    };
    
    class B
    {
      void foo();
    };
    
    void foo();
    
    // Implementation File
    void foo()
    {
    }
    

    Which scope should the definition apply to? C++ does not restrict you from having different functions with the same names in different scopes, so you have to tell the compiler what function you're defining.

    0 讨论(0)
  • 2020-11-27 22:01

    EDIT: I misread your question. This would be an answer to the question whether you can split header-files. It doesn't help you to avoid using LongClassName::-syntaxes, sorry.

    The simple answer: You can split up c++-file, but you can not split up header-files.

    The reason is quite simple. Whenever your compiler needs to compile a constructor, it needs to know exactly how many memory it needs to allocate for such an object.

    For example:

    class Foo {
       double bar;  //8 bytes
       int goo;  //4 bytes
    }
    

    new Foo() would require the allocation of 12 bytes memory. But if you were allowed to extend your class definitions over multiple files, and hence split header files, you could easily make a mess of this. Your compiler would never know if you already told it everything about the class, or whether you did not. Different places in your code could have different definitions of your class, leading to either segmentation faults or cryptic compiler errors.

    For example:

    h1.h:

    class Foo {
       double bar;  // 8 bytes
       int goo;     // 4 bytes
    }
    

    h2.h: #include "h1.h"

    class Foo {
       double goo;   // 8 bytes
    } // we extend foo with a double.
    

    foo1.cpp:

    #include "foo1.h"
    
    Foo *makeFoo() {
       return new Foo();
    
    }
    

    foo2.cpp:

    #include "foo2.h"
    
    void cleanupFoo(Foo *foo) {
       delete foo;
    }
    

    foo1.h:

    #include "h1.h"
    
    Foo *makeFoo();
    

    foo2.h:

    #include "h1.h"
    #include "h2.h"
    
    void cleanupFoo(Foo *foo)
    

    main.cpp:

    #include foo1.h
    #include foo2.h
    
    void main() {
        Foo *foo = makeFoo();
        cleanupFoo(foo);
    }
    

    Carefully check what happens if you first compile main.cpp to main.o, then foo1.cpp to foo1.o and foo2.cpp to foo2.o, and finally link all of them together. This should compile, but the makeFoo() allocates something else then the cleanupFoo() deallocated.

    So there you have it, feel free to split .cpp-files, but don't split up classes over header files.

    0 讨论(0)
  • 2020-11-27 22:03

    If you want to avoid typing the "Graph::" in front of the printGraph, addEdge etc., then the answer is "no", unfortunately. The "partial class" feature similar to C# is not accessible in C++ and the name of any class (like "Graph") is not a namespace, it's a scope.

    0 讨论(0)
  • 2020-11-27 22:05

    No there's not. Not directly at least. You could go for preprocessor tricks, but don't do it.

    #define IMPL Graph::
    
    IMPL Graph(int n){}
    void IMPL printGraph(){}
    void IMPL addEdge(){}
    void IMPL removeEdge(){}
    

    Also, you shouldn't even want to do it. What's the point. Besides it being a C++ rule, it lets you know you're actually implementing a member function.

    0 讨论(0)
提交回复
热议问题