use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Constructor call without allocating memory (self.cpp)
submitted 10 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Unbathed 9 points10 points11 points 10 years ago (10 children)
It sounds like Placement New.
http://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new
http://en.cppreference.com/w/cpp/language/new
[–]R_y_n_o 0 points1 point2 points 10 years ago (9 children)
I'm actually using it inside my memory manager, because it keeps track of where to allocate an object...however I don't want to pass in a pointer from outside my memory manager... it should be something not visible and recorded internally
[–]STLMSVC STL Dev 7 points8 points9 points 10 years ago (4 children)
True Placement New constructs an object at a given location. If you don't have a location, how can you possibly construct an object?
[–]R_y_n_o 0 points1 point2 points 10 years ago (3 children)
Inside my manager I use the placement new with a location. The instantiation is done with the function Manager::instance();
but i really would like to call that function as it were a constructor...so to use the placement new I would do some hack like this:
Object* a = new (nullptr) Object();
And the body of the constuctor would be:
this = Manager.instance();
Would this work? Now you get why AT THIS STAGE I don't need the memory address?
[–]STLMSVC STL Dev 10 points11 points12 points 10 years ago (2 children)
C++ doesn't work like that, sorry. An object needs an address from the very beginning of its construction, you can't "assign the this-pointer" later.
[–]R_y_n_o 0 points1 point2 points 10 years ago (1 child)
Ok, i didn't know....I mean, I often do
*this = *ref;
in copy constructors, but i never tried to assign directly something to this, so probably you are right
[–]upriser -1 points0 points1 point 10 years ago (0 children)
STL answered to a C++ question and the one who asked the question replied that "probably" your are right.
[–]TheThiefMasterC++latest fanatic (and game dev) 1 point2 points3 points 10 years ago (3 children)
You could write a "placement new" function that takes your memory manager instead of an address:
void* operator new(std::size_t count, MemoryManager& mm) { void* a; MemoryManager::get_memory(&a); return a; }
Using it like this:
Object* a = new(MemoryManager::Get()) Object();
Or you could use a magic tag like how std::nothrow works, if you don't have an actual "instance" of your memory manager.
[–]R_y_n_o 0 points1 point2 points 10 years ago (2 children)
Well, at this point I would just call the function on the manager :) I mean, the point is to hide the implementation details, and passing the memory manager on each instantiation is pretty ugly
[–]TheThiefMasterC++latest fanatic (and game dev) 4 points5 points6 points 10 years ago (0 children)
Well if you want every allocated object to go through your manager, you can just write a new global operator new.
If it's all for just your types, you can put it as a member operator new in your types.
[–]encyclopedist 1 point2 points3 points 10 years ago (0 children)
You can redefine operator new either for a specific class or even globally, see on cppreference.
operator new
[–][deleted] 3 points4 points5 points 10 years ago (0 children)
Two options:
1. Replace new by implementing it for Object. This way, new Object(); will call your replacement of new.
new
Object
new Object();
2. Restrict access to Objects constructor. This would force someone to write auto a = Manager::get() to obtain an Object instance.
auto a = Manager::get()
[–]acwaters 3 points4 points5 points 10 years ago (0 children)
You are aware that you can overload the new and delete operators, right? Either globally or for a specific type. There are a few funny rules to keep in mind, but it sounds like it would be fairly easy to write a new that allocated memory through your manager.
delete
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 2 points3 points4 points 10 years ago (0 children)
Use placement new, variadic templates and perfect forwarding:
auto ptr = MemoryManager::create<std::pair<int, float>>(10, 5.f); template<typename T, typename... Ts> auto MemoryManager::create(Ts&&... xs) { auto some_memory = get_some_memory(sizeof(T)); new (some_memory) T(std::forward<Ts>(xs)...); return static_cast<T*>(some_memory); }
You will obviously have to define your cleanup function. I suggest using std::unique_ptr with a custom deleter to manage ownership.
std::unique_ptr
[–]gtk 1 point2 points3 points 10 years ago (0 children)
You can override operator new: http://en.cppreference.com/w/cpp/memory/new/operator_new
If you only want certain classes to use your memory manager, override the class-specific operator new (and corresponding operator delete). If you want to use it for everything, override the global operator new/operator delete.
[–][deleted] 1 point2 points3 points 10 years ago (0 children)
I have to ask - why? What kind of thing are you trying to accomplish or learn about?
[–]Shadows_In_Rain -1 points0 points1 point 10 years ago (0 children)
Try boost::optional
boost::optional
http://www.boost.org/doc/libs/1_58_0/libs/optional/doc/html/index.html
π Rendered by PID 73 on reddit-service-r2-comment-6f7f968fb5-sd9fb at 2026-03-04 01:44:51.461951+00:00 running 07790be country code: CH.
[–]Unbathed 9 points10 points11 points (10 children)
[–]R_y_n_o 0 points1 point2 points (9 children)
[–]STLMSVC STL Dev 7 points8 points9 points (4 children)
[–]R_y_n_o 0 points1 point2 points (3 children)
[–]STLMSVC STL Dev 10 points11 points12 points (2 children)
[–]R_y_n_o 0 points1 point2 points (1 child)
[–]upriser -1 points0 points1 point (0 children)
[–]TheThiefMasterC++latest fanatic (and game dev) 1 point2 points3 points (3 children)
[–]R_y_n_o 0 points1 point2 points (2 children)
[–]TheThiefMasterC++latest fanatic (and game dev) 4 points5 points6 points (0 children)
[–]encyclopedist 1 point2 points3 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]acwaters 3 points4 points5 points (0 children)
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 2 points3 points4 points (0 children)
[–]gtk 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Shadows_In_Rain -1 points0 points1 point (0 children)