2
2005
If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?
Thanks
/ say the "new" operator etc?
Thanks
If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?
2005 said:If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?
Thanks
You don't need the new operator to allocate - in a perfect world, using
new should be discouraged.
A ctor's primary purpose is to allocate an instance based on the
contents of its type (ignoring inherited classes for now) and whatever
resources are required.
In C++ it happens to have a secondary purpose that is to initialize
members, if any, using an init list.
The initialisation is not a
requirement but too often overlooked and quite usefull.
To answer your question: allocation happens when the constructor is
invoked.
and "invoke" does not mean "call".
#include <iostream>
class N
{
int n;
public:
N() : n(0) { std::cout << "invoke ctor\n"; }
~N() { std::cout << "invoke d~tor\n"; }
const int& get() const { return n; }
};
int main()
{
N instance; // allocation happens here
std::cout << "instance.n = " << instance.get() << std::endl;
return 0;
} // deallocation happens here - at end of scope
/*
invoke ctor
instance.n = 0
invoke d~tor
*/
The explanation is in the FAQ:
http://www.parashift.com/c++-faq-lite/ctors.html
Does it mean that if we use a constructor, we don't need a "new"
operator?
Sorry about it - will follow it in the future.Kai-Uwe Bux said:Please do not top-post in this group. It's frowned upon.
2005 wrote [top-posting and sig-quoting corrected]
You say "Sorry, that's incorrect." - Could you pls give explanations?
Alf said:* 2005:
Yes, if you'd care to respect the conventions of the group¹: don't
top-post, don't quote signatures, quote only relevant things, but do
quote the relevant things, ask specific questions (about what you quoted
right above the specific questions), and use plain English.
Otherwise this could amount to a fullblown tutorial on basic C++.
And I've already written that² -- and besides, there isn't room enough
in a Usenet article.
Notes:
¹ In an earlier article in this thread, in reply to Kai-Uwe Bux' request
for the same, you wrote «Sorry about it - will follow it in the
future.», but you didn't.
Alf said:And I've already written that² -- and besides, there isn't room enough
in a Usenet article.
Notes:
¹ In an earlier article in this thread, in reply to Kai-Uwe Bux' request
for the same, you wrote «Sorry about it - will follow it in the
future.», but you didn't.
² See the FAQ item titled «What other "newbie" guides are there for
me?», currently at <url:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.21>.
Not True; Pls look at the time stamp.
I replied you before I replied to Kai-Uwe Bux' request - makes sense
Are you the author of "www.parashift.com"
You say "Sorry, that's incorrect." - Could you pls give explanations?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.