开发者

Why is the syntax the way it is for implementing classes in c++?

I tend to write a class declaration in a source file and then decide I want to export it in a header. But this always means that I have to split it up and edit almost every function so that the class is declared in the header, and I have to put the functions individually in the cpp file.

Why is it that I cannot leave the class A { in the source file so I can avoid adding A:: at every function? The compiler just as开发者_运维问答sumes it's a redefinition of A. What is the reason that the compiler doesn't simply acknowledge that I am describing the same A?

Also, partly related: Is there a way for me to hide the private variables of a class in the source file rather than putting it in the header? The header is used to export functionality of a class to other parts of a program, to which I do not need to reveal my private members. Yet I don't believe I can declare them in the implementation cpp because I can't redefine the class there, I can only implement functions in there.


I tend to write a class declaration in a source file and then decide I want to export it in a header. But this always means that I have to split it up and edit almost every function so that the class is declared in the header, and I have to put the functions individually in the cpp file.

If you always write the class definition and member function definitions separately — even when in the same file — then this "problem" immediately disintegrates into oblivionness:

Same file

// myClass.cpp

struct myClass {
   void f();
};

void myClass::f() {
   // ...
}

Now split up

// myClass.hpp

struct myClass {
   void f();
};

// myClass.cpp

#include "myClass.hpp"

void myClass::f() {
   // ...
}

Easy!


Why is it that I cannot leave the class A { in the source file so I can avoid adding A:: at every function? The compiler just assumes it's a redefinition of A. What is the reason that the compiler doesn't simply acknowledge that I am describing the same A?

That's just how the language is. A class definition is self-contained and unique and must contain all member declarations. I guess it would be too complex to be able to split it up; "that's just the way it is" is about as much of a proven rationale as you're going to get here, though.


Is there a way for me to hide the private variables of a class in the source file rather than putting it in the header?

Look up the PIMPL idiom.


I assume you are talking about something like this:

Header file

class Foo {
public:
   do_something_with_foo() const;
   do_something_to_foo();
private;
   // Instance members elided
};

Source file

#include "foo.hh"

class Foo {
   do_something_with_foo() const {
      // body elided
   }

  do_something_foo() {
      // body elided
   }
};

Which is of course illegal. The reason is that you can't define the class twice. The second looks just like a definition. Some other syntax is needed to identify that what is being defined is a class member function. Tomalak's answer shows how to do that.


C++ has inherited a rather antiqued header inclusion mechanism from C.

Due to how it works, if you put everything in your header, the compiler will have to recompile those functions every time you include that file — this slows down compilation significantly.

Why does that happen? When you include a header, the code is literally copy-pasted into the source file. The compiler doesn't know where it originally comes from, so it cannot know that those functions might have already been compiled before.

It's not so bad though: the extra code is removed in the linker phase. The linker can identify the same function when it appears in multiple places, and remove the excess code.

By having the header include only the declaration of the class and not the definition as well, you save the compiler some work. It only compiles those functions when it processes the class source file and that's it.

Is there a way for me to hide the private variables of a class in the source file rather than putting it in the header?

There is no mechanism offered by the language itself to do this, but you can take a look at the Pimpl Idiom. But don't start using it like crazy, you might just be over-engineering if all you want to do is remove the private fields from the header.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜