473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

if Constructor can be used to initialize ...



If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?

Thanks

Oct 25 '06 #1
14 1610
* 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?
Oct 25 '06 #2
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?
Oct 25 '06 #3

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

Oct 25 '06 #4
* 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?
Oct 25 '06 #5
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?
Oct 25 '06 #6
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
Oct 25 '06 #7

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?
Oct 25 '06 #8

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.

Oct 25 '06 #9
* 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.com/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?
Oct 25 '06 #10

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.com/c++-faq-lite/newbie.html#faq-29.21>.
Oct 25 '06 #11

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.com/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

Oct 25 '06 #12
* 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?
Oct 26 '06 #13
* 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.com/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?
Oct 26 '06 #14
* 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?
Oct 26 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
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;
10
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...
8
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...
7
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...
3
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...
4
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...
3
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 {
74
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...
11
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.