开发者

How to order my objects in a C++ class correctly

开发者 https://www.devze.com 2022-12-22 01:59 出处:网络
I have been coding regurlarly in C++ in the past months. I am getting used to it step by step... but there are things that confuse me about formatting.

I have been coding regurlarly in C++ in the past months. I am getting used to it step by step... but there are things that confuse me about formatting.

I know there is a lot of legacy from C that I supousee mixes with C++. This time I have doubts about how to order properly my members and functions within in a class. Also considering their access modifiers.

How is the convention in this? Until k开发者_如何学Cnow I am doing everything "public" and writing first constructor of class, then destructor, next members and finally functions. It this correct? What happens when introducing "private" and "protected" access modifiers or "virtual" functions?

From the documents I have look in the Internet there is different ways of doing things. But my questions aims to get the knowledge from a community that develops in C++ that I want to blend into. ;-)

Thanks a lot!!!


My humble opinion, after having read many style guides all over the 'net:

  • Public first, because that is the interface of your class, which people want to see first.
  • From the same reasoning, private goes last.
  • If you have any private functions, place them before private members. (Again, same reasoning. Your members are of the least interest to anyone.)
  • Constructor first in the public section, because people have to call that before they have an object on which to invoke any functions.
  • Destructor right after the constructor, just to have them in one place.
  • Within the public / protected / private sections, find some grouping logical to any users of the library, and write a one-line comment in front of each group. (Doesn't matter that much what's the logic, as long as it's documented.)
  • Don't make any rules more complicated than this, because the more complicated, the easier to get it wrong (or just ignore it as inconvenient).

Remember that members should be initialized in the order they are declared, and destroyed in the reverse order.


You answered your question yourself: "there is different ways of doing things". Google has published C++ Style Guide which you could use as starting point.


I'd suggest if you are learning, you consider making everything private to begin with, and then revising this as needed. That way, you will have to think about each thing when its needed outside the class and may spot a better way to structure your classes as a result.

Let us recommend a book called Code Complete, just search Amazon or your favourite online book store.


Until know I am doing everything "public"

Don't. When you create a class it is like creating your own type, just like int, char, etc.

You have to strive to present an interface ( the public part of your C++ class ) that is easy to use and encapsulate the implementation ( the private part of your class ). If your class does not have any private members then you are not encapsulating anything and is considered bad design.

Read this series of articles on object oriented design principles. You may have a hard time reading it but read it multiple times till you feel you have got a hang of the concepts.


The order does not matter.

( The only exception to this is that members are initialized in the order they are mentioned, and destroyed in the reverse order. This will only be of significance if your constructors and destructors have side effects - which is rare and best avoided if possible. )

So you can place things in the order that pleases you.

It is best to keep to a consistent order, so you can find things in a class you wrote months ago.

I like to place everything private at the end, so it is tucked out of sight from the casual inspection.

0

精彩评论

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

关注公众号