If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?
Thanks
Oct 25 '06
14 1651
Alf P. Steinbach wrote:
* 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.
Not True; Pls look at the time stamp.
I replied you before I replied to Kai-Uwe Bux' request - makes sense
² 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>.
Alf P. Steinbach wrote:
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>.
Are you the author of "www.parashift. com" or
"www.parashift. com/c++-faq-lite/newbie.html#faq-29.21"?
Thanks
* 2005:
Alf P. Steinbach wrote:
>* 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.
Not True; Pls look at the time stamp.
I replied you before I replied to Kai-Uwe Bux' request - makes sense
Ah, well, sorry, I'll do a summary: look elsethread.
--
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:
Alf P. Steinbach wrote:
>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>.
Are you the author of "www.parashift. com"
No, that's Marshall Cline.
or "www.parashift. com/c++-faq-lite/newbie.html#faq-29.21"?
Of the tutorial referred to there.
--
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:
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++ constructors don't allocate memory for the object they're
constructing (they can allocate memory for members). The main purpose
of a C++ constructor is to couple initializion to object creation, so
that you can't have one without the other ("creation": that the object
becomes available for use). A simple static local variable is an
example where allocation in practice happens way long before the object
is created: the object is created the first time execution passes the
declaration, the memory for the object is reserved at program startup.
>>In C++ it happens to have a secondary purpose that is to initialize members, if any, using an init list.
Sorry, that's incorrect.
See above.
>>The initialisation is not a requirement but too often overlooked and quite usefull.
Sorry, that's incorrect.
See above.
>>To answer your question: allocation happens when the constructor is invoked.
Sorry, that's incorrect.
See above.
>>and "invoke" does not mean "call".
Sorry, that's incorrect.
In the standard and common usage the terms "invoke" and "call" are used
interchangeably .
>>#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).
Formally the compiler is allowed to allocate memory for N any time
before the object is created ("created": becomes available for usage).
>> 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).
See above.
>>/* 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).
As said.
--
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s 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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 project—planning, coding, testing, and deployment—without 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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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.
| |