3 ms·
You can persuade the C++ constructor to run on an arbitrary address trivially with "placement-new". See here https://en.wikipedia.org/wiki/Placement_syntax htt
by anonymous 13y ago
You can persuade the C++ constructor to run on an arbitrary address trivially with "placement-new". See here
https://en.wikipedia.org/wiki/Placement_syntax https://en.wikipedia.org/wiki/Placement_syntax . In short, you can do:
void* mem;
... snip ...
MyClass* thing = new (mem) MyClass;
And that doesn't allocate memory, but calls the constructor on the mem pointer. You are responsible for having allocated enough memory. You cannot call delete normally on this object though. You'll need to call the destructor manually as such:
thing->~MyClass();
before the memory is deallocated. If the destructor doesn't actually do anything, you can safely skip this.
- tehwalrus 13y agoThanks, that's very helpful! I am blissfully unaware of almost all C++ specific syntax beyond basic class definitions, as I use it to add the odd syntactic construct to C (like operator overloads for + etc for my math 2- and 3-vectors) so I had no idea of this.
- anonymous 13y agoIf you want to learn more, I'd suggest at first reading (or at least skimming) Bjarne Stroustrup's "The C++ programming language". Also, the C++ FAQ http://www.parashift.com/c++-faq/ http://www.parashift.com/c++-faq/ is useful for getting an overview on the corner cases of C++. After which you should visit its inverse, the C++ FQA (frequently questioned answers, i.e. an anti-C++ answer to the FAQ) http://www.yosefk.com/c++fqa/ http://www.yosefk.com/c++fqa/ to learn why no-one in their right mind would ever use C++. That won't teach you C++ per se, but would give you a useful look at the breadth of the language and how and why things happen the way they do.
- tehwalrus 13y agoThanks. I have got a copy of a Stroustrup on my shelf (I think it's a standard library reference, not the language definition) and I've been developing in "C with classes" for a while, so I've discovered quite a few of the pitfalls. I prefer to let clang++ point out when I stumble across something insane, like: vector<vector<T>> // wrong, uses a >> operator?! vector<vector<T> > // compiles correctly. In some ways I prefer what Objective C did, as a proper superset, and in others I'm drawn to projects like Cello[1]. I'm happy with using (and knowing only) a very small set of C++ features, especially where they're useful for defining simple algebras for objects! I'm sure I'll find another feature I like one day - this one (GGP) may be one of them. There is one OSS project I know that I never want to emulate: the code literally fails to compile via the python installer until you have run the makefile at least once, by which time it's finished generating all the header files. Whiskey Tango Foxtrot! [1] https://github.com/orangeduck/libCello https://github.com/orangeduck/libCello
- mikeash 13y agoI'd like to mention that, while the FQA pulls no punches and is often sarcastic or worse, it's still a great read and will teach you a lot about C++ even if you disagree with the idea that nobody should use the language. Don't be scared off by that "no-one in their right mind" thing. Although, if you read through it and subsequently decide that C++ is not the best language to use, I wouldn't call that the worst outcome....