English 中文(简体)
C++ IDE that supports Scott Meyer s advice: Prefer non-member non-friend functions over members
原标题:

Scott Meyer s argument that non-member functions increase encapsulation and allow for more elegant design (designwise) seems very valid to me. See here: Article

Yet I have problems with this. (And seemingly others too, especially Library developers, who usually completely ignore this)

Code usually looks better and more logical when I use member functions. This may be an acquired taste though and just takes some getting used to looking at algorithms first and then on the objects. (shudder)

So maybe I have only one problem:

With member functions, me AND my IDE know what the class can do.

For me this is huge! I use nothing that doesn t support member function code completion for programming. In well designed libraries it completely replaces documentation for me. And even if I would look at the api doc, looking through the member list just feels absolutely natural, logical and I can be sure that, well, this is the end. If the method is not in there, I can safely assume it doesn t exist and I can write my non-member non-friend.

I put up with this in the STL, because, well, it makes sense to see algorithms apart from basic components and because of the you get used to it factor.

I haven t seen an IDE that can tell me what non-member functions work on a particular class.

And this actually is my question: Is there an IDE (or IDE feature) out there that helps with this code convention?

问题回答

I ve come across this thing in the past.

My idea then was rather clumsy, but got the job done: namespaces.

What I did was

namespace myclass
{
    class MyClass
    {
        ...
    };

    MyClass operator+(const MyClass& lhs, const MyClass& rhs){...}
}

Meyers is certainly correct that using non-members increases encapsulation, by minimising the number of functions that could potentially access the private state. However, encapsulation is just one of many (often conflicting) aspects of code quality.

He does make a valid point that the library writer won t necessarily write functions for every possible usage of the class (since there may be usages that they don t think of). This means that you may well have to add non-member "helper" functions yourself, just as they would do if they followed Meyers s advice. So there s no way of knowing that the set of member and friend functions is indeed the only set of functions that act on the class.

As a technoluddite, the "IDE" that I favour (a text editor and a shell) has the following "feature" that s pretty good for finding the functions acting on a class:

find . -name  *.h  -o -name  *.cpp  | xargs grep MyClass

I can t comment on "real" IDEs.

I don t believe it is possible for an IDE to tell you all non-member functions that you can use with your class. Using templates, it is simply too difficult to make a list of all such functions. IMO, the best you can hope for is for an IDE to be able to tell you before compilation whether a call you re trying to make is valid. And even that requires some serious compilation-like process inside the IDE.

I understand how you use member functions as a replacement for documentation in classic classes. But the design Scott Meyer suggests isn t about classes that provide complex functionalities, just basic ones. Complex functionalities come from elsewhere, the original class may or may not know about it, it does not really matter. It s all part of the idea. But you are right. In that case, there is a renewed need for well-thought documentation.

Try to use Visual AssistX, it has this nice feature: Right click on your class, Refactor (VA X) -> Find references. It actually works.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签