If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?
Thanks 14 1648
* 2005:
>
If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?
Sometime before the constructor is called.
For the "new" operator that's immediately before.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Does it mean that if we use a constructor, we don't need a "new"
operator?
Thanks
Alf P. Steinbach wrote:
* 2005:
If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?
Sometime before the constructor is called.
For the "new" operator that's immediately before.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
2005 wrote:
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
* Salt_Peter:
2005 wrote:
>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.
Sorry, that's meaningless.
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.
Sorry, that's incorrect.
In C++ it happens to have a secondary purpose that is to initialize
members, if any, using an init list.
Sorry, that's incorrect.
The initialisation is not a
requirement but too often overlooked and quite usefull.
Sorry, that's incorrect.
To answer your question: allocation happens when the constructor is
invoked.
Sorry, that's incorrect.
and "invoke" does not mean "call".
Sorry, that's incorrect.
#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
Sorry, that's incorrect (although in practice true).
std::cout << "instance.n = " << instance.get() << std::endl;
return 0;
} // deallocation happens here - at end of scope
Sorry, that's incorrect (although in practice true).
/*
invoke ctor
instance.n = 0
invoke d~tor
*/
The explanation is in the FAQ: http://www.parashift.com/c++-faq-lite/ctors.html
Sorry, that's incorrect (this FAQ section does not concern allocation,
and provides no explanation of when allocation occurs).
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Anyway, I think the faq-list is exactly what 2005 needs.
Alf P. Steinbach 写道:
* Salt_Peter:
2005 wrote:
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.
Sorry, that's meaningless.
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.
Sorry, that's incorrect.
In C++ it happens to have a secondary purpose that is to initialize
members, if any, using an init list.
Sorry, that's incorrect.
The initialisation is not a
requirement but too often overlooked and quite usefull.
Sorry, that's incorrect.
To answer your question: allocation happens when the constructor is
invoked.
Sorry, that's incorrect.
and "invoke" does not mean "call".
Sorry, that's incorrect.
#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
Sorry, that's incorrect (although in practice true).
std::cout << "instance.n = " << instance.get() << std::endl;
return 0;
} // deallocation happens here - at end of scope
Sorry, that's incorrect (although in practice true).
/*
invoke ctor
instance.n = 0
invoke d~tor
*/
The explanation is in the FAQ: http://www.parashift.com/c++-faq-lite/ctors.html
Sorry, that's incorrect (this FAQ section does not concern allocation,
and provides no explanation of when allocation occurs).
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Please do not top-post in this group. It's frowned upon.
2005 wrote [top-posting and sig-quoting corrected]
>
Alf P. Steinbach wrote:
>* 2005:
>
If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?
Sometime before the constructor is called.
For the "new" operator that's immediately before.
Does it mean that if we use a constructor, we don't need a "new"
operator?
An object is a region of storage [1.8/1]. Thus, before you initialize an
object, you need to have that region of memory. Now, how you get that
region of storage, depends on how the object is created (not initialized).
An object is created by a definition, by a new-expression, or by the
implementation when needed [1.8/1]. Examples:
a) Defining a variable:
SomeClass some_var ( some_args );
Memory is allocated automagically by the compiler. Then the object is
created.
b) Dynamic allocation via new:
SomeClass* some_ptr = new SomeClass ( some_args );
Here, new will allocate memory by calling an allocation function. Then the
constructor is called to initialize the object.
c) Creating a temporary:
some_function( SomeClass( some_args ) );
Memory allocation happens without you doing anything.
Best
Kai-Uwe Bux
You say "Sorry, that's incorrect." - Could you pls give explanations?
Alf P. Steinbach wrote:
* Salt_Peter:
2005 wrote:
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.
Sorry, that's meaningless.
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.
Sorry, that's incorrect.
In C++ it happens to have a secondary purpose that is to initialize
members, if any, using an init list.
Sorry, that's incorrect.
The initialisation is not a
requirement but too often overlooked and quite usefull.
Sorry, that's incorrect.
To answer your question: allocation happens when the constructor is
invoked.
Sorry, that's incorrect.
and "invoke" does not mean "call".
Sorry, that's incorrect.
#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
Sorry, that's incorrect (although in practice true).
std::cout << "instance.n = " << instance.get() << std::endl;
return 0;
} // deallocation happens here - at end of scope
Sorry, that's incorrect (although in practice true).
/*
invoke ctor
instance.n = 0
invoke d~tor
*/
The explanation is in the FAQ: http://www.parashift.com/c++-faq-lite/ctors.html
Sorry, that's incorrect (this FAQ section does not concern allocation,
and provides no explanation of when allocation occurs).
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Kai-Uwe Bux wrote:
Please do not top-post in this group. It's frowned upon.
2005 wrote [top-posting and sig-quoting corrected]
Sorry about it - will follow it in the future.
Thanks for letting me know.
* 2005:
You say "Sorry, that's incorrect." - Could you pls give explanations?
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.
See the FAQ item titled What other "newbie" guides are there for
me?, currently at <url:
http://www.parashift.c om/c++-faq-lite/newbie.html#faq-29.21>.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kurt Krueckeberg |
last post by:
Given a class X
class X {
public: X(int);
X(const X&); //. . .
};
Is this line
X x1(1);
the same thing as
X x2 = 2;
|
by: John Brock |
last post by:
I have a base class with several derived classes (I'm writing in
VB.NET). I want each derived class to have a unique class ID (a
String), and I want the derived classes to inherit from the base
class *Shared* methods which make use of the class ID.
So I gave the base class a classID field, and then I gave the
derived classes Shared constructors, which I used to set the classID
field to the appropriate values for each derived class. But...
|
by: Sam Kuehn |
last post by:
How do I accomplish the fallowing (is it even possible). Say I write a UserControl
"MyControl.ascx". Now I use LoadControl("MyControl.ascx"). But I really
want MyControl to require parameters in the constructor for example MyContorl
oMyControl = new MyContorl(employeeid). However I need to load the control
at runtime so the have to call it this way LoadControl("MyControl.ascx")
and I get an error that I have not supplied any parameter...
|
by: Doug |
last post by:
Hi
I am working through a learning module privately where there is discussion
about overloading the constructor.
Could someone please let me know the benefit or purpose of doing this in a
class?
Thanks
|
by: swengtoo |
last post by:
In his book "More Effective C++", Scott Meyer suggests in "Item 4" to
"Avoid gratuitous default constructors".
To summarize his claims against default constructors for "the right
classes" he states that:
================== START QUOTE =============
"A default constructor is the C++ way of saying you can get something
for nothing. Constructors initialize objects, so default constructors
initialize objects without any information from...
| |
by: david |
last post by:
Hello,
I'd like to know whether the compiler can detect a member variable which is
not initialized (on purpose) by the constructor.
Easier with an simple example (the class is supposed to be a fixed point
variable):
class A
{
|
by: hurcan solter |
last post by:
Consider the code fragment;
class A
{
public:
A(){}
A(int prm):mprm(prm){}
int mprm;
};
class B:public A
{
|
by: Zytan |
last post by:
I have a struct constructor to initialize all of my private (or public
readonly) fields. There still exists the default constructor that sets
them all to zero. Is there a way to remove the creation of this
implicit default constructor, to force the creation of a struct via my
constructor only?
Zytan
|
by: Dijkstra |
last post by:
Hi folks!
First, this is the code I'm using to expose the problem:
------------------------------------------------------------------
#include <functional>
#include <string>
#include <iostream>
using namespace std;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |