> Realizing that C++’s “special” member functions may be declared private Don't do this anymore! Just delete them: MyClass& operator=(const MyClass&) = delete; Private-really-means-deleted was a cool and very useful trick before C++11. So useful, that it was a shame to require secret knowledge and aha moments to use. So they just made it a normal feature of the language. There's also a way to explicitly use the defau…
There is an obscure pitfall of making a class noncopyable by making the copy constructor private. Consider the following class: class X { private: X& (const X&); // not defined public: X bar() { X x = ......; return x; } }; Due to programmer error, an instance of X is returned by value despite the class being noncopyable. The copy constructor being private doesn't help as bar() is a method of X. This will compile and…
My Most Important C++ Aha Moments (2006)
21–30 of 84 posts
Re: My Most Important C++ Aha Moments (2006)
#22> Visitor lets you define a new operation without changing the classes of the elements on which it operates. Yes, but that's not the real point. You can do that in other ways. Creating a new std::algorithm does that, for example. The real purpose is letting one function be polymorphic in multiple directions. This is called multiple dispatch. Some languages support this as a built-in feature, but C++ doesn't, so a cou…
http://eli.thegreenplace.net/2016/a-polyglots-guide-to-multi...
Re: My Most Important C++ Aha Moments (2006)
#23I love C and Objective-C. I can't stand C++. Is that common among programmers?
What has always bugged me about C++, but maybe I am overlooking some reason why this generally cannot work, is that it does not resolve circular dependencies such as this one: // file: A.h class A { B* _b; }; // file: B.h class B { A* _a; }; I mean a pointer has a fixed size, so couldn't the compiler just leave some sort of type placeholder in class A until class B is eventually defined.
class B;
class A {
B* _b;
};
and it'll be happy.Re: My Most Important C++ Aha Moments (2006)
#24I love C and Objective-C. I can't stand C++. Is that common among programmers?
What has always bugged me about C++, but maybe I am overlooking some reason why this generally cannot work, is that it does not resolve circular dependencies such as this one: // file: A.h class A { B* _b; }; // file: B.h class B { A* _a; }; I mean a pointer has a fixed size, so couldn't the compiler just leave some sort of type placeholder in class A until class B is eventually defined.
// file: A.h
class B;
class A {
B* _b;
};
// file: B.h
class B {
A* _a;
};Re: My Most Important C++ Aha Moments (2006)
#25> Realizing that C++’s “special” member functions may be declared private Don't do this anymore! Just delete them: MyClass& operator=(const MyClass&) = delete; Private-really-means-deleted was a cool and very useful trick before C++11. So useful, that it was a shame to require secret knowledge and aha moments to use. So they just made it a normal feature of the language. There's also a way to explicitly use the defau…
> Private-really-means-deleted was a cool and very useful trick before C++11 Rather, until every compiler you need to target supported this specific C++11 feature. In other words, it's still a cool and very useful trick ;)
Re: My Most Important C++ Aha Moments (2006)
#26I love C and Objective-C. I can't stand C++. Is that common among programmers?
It's doesn't hurt either that the libraries available in C++ are really cool.
Re: My Most Important C++ Aha Moments (2006)
#27I love C and Objective-C. I can't stand C++. Is that common among programmers?
The more I learn C++, the more I can't stand it. Its ever-increasing complexity keeps being a distraction from the actual problem you're trying to solve.
Re: My Most Important C++ Aha Moments (2006)
#28Re: My Most Important C++ Aha Moments (2006)
#29I love C and Objective-C. I can't stand C++. Is that common among programmers?
C programmer here; I don't like C++ much because every library tends to reinvent the whole stack (or maybe that's just my impression from using MFC and Qt, which are notorious in that regard). RAII and scope based construction/destruction is actually quite nice.
Re: My Most Important C++ Aha Moments (2006)
#30> Realizing that C++’s “special” member functions may be declared private Don't do this anymore! Just delete them: MyClass& operator=(const MyClass&) = delete; Private-really-means-deleted was a cool and very useful trick before C++11. So useful, that it was a shame to require secret knowledge and aha moments to use. So they just made it a normal feature of the language. There's also a way to explicitly use the defau…
Anyway, declaring a constructor " = delete" isn't the same thing as making it private. If it's private, the code in your class scope can still use it. If you can't trust your class private code to do things correctly, then you're screwed; go join IT and write backup shell scripts. So basically this "= delete" is just another ear growing out of the elbow of C++.