473,414 Members | 1,723 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,414 software developers and data experts.

Accerated C++ question

Dear all,

Sorry for the first mail, I pressed enter by mistake.

On page 190,

template <class T> class Vec{

public:
Vec() {create();}
explicit Vec(size_type n, const T& val=T()){create(n,val)}

..
..
..

Could anyone clarify the default argument for me

const T& val=T()

T is the type, but I could not understand T(), I went bak to function
templates and checked if I missed sth. but could not find.

The explanation on the next page is
"Note that it uses a default argument (§7.3/127) for the second
parameter. Thus, the constructor effectively defines two constructors:
One takes a single argument of type size_type; the other takes a
size_type and a const T&. In both cases we call a version of create
that takes a size and a value. We'll assume that this function, which
we'll write in §11.5/203, will allocate enough memory to hold n
objects of type T, and will give those elements the initial value
specified by val."

Many thx.

Apr 21 '06 #1
11 1589
T gets replaced with the template class so for instance

class MyClass
{
public:
MyClass();
};

Vec<MyClass> Vector( 5 );

would make a call to MyClass() for the temporay second parameter
It would have been the same as you would have specified:

Vec<MyClass> Vector( 5, MyClass() );

--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"utab" <um********@gmail.com> wrote in message
news:11*********************@z34g2000cwc.googlegro ups.com...
Dear all,

Sorry for the first mail, I pressed enter by mistake.

On page 190,

template <class T> class Vec{

public:
Vec() {create();}
explicit Vec(size_type n, const T& val=T()){create(n,val)}

..
..
..

Could anyone clarify the default argument for me

const T& val=T()

T is the type, but I could not understand T(), I went bak to function
templates and checked if I missed sth. but could not find.

The explanation on the next page is
"Note that it uses a default argument (§7.3/127) for the second
parameter. Thus, the constructor effectively defines two constructors:
One takes a single argument of type size_type; the other takes a
size_type and a const T&. In both cases we call a version of create
that takes a size and a value. We'll assume that this function, which
we'll write in §11.5/203, will allocate enough memory to hold n
objects of type T, and will give those elements the initial value
specified by val."

Many thx.
Apr 21 '06 #2
On 21 Apr 2006 02:41:19 -0700, "utab" <um********@gmail.com> wrote:
Dear all,

Sorry for the first mail, I pressed enter by mistake.

On page 190,

template <class T> class Vec{

public:
Vec() {create();}
explicit Vec(size_type n, const T& val=T()){create(n,val)}

.
.
.

Could anyone clarify the default argument for me

const T& val=T()

T is the type, but I could not understand T(), I went bak to function
templates and checked if I missed sth. but could not find.

<....>

T() is an element constructed with a constructor that takes no
parameters, or a constructor thathas default values for all
parameters. If there is no such constructor, the compiler will issue
an error.

For integral types, it is equivalent to assigning it a 0.

regards,

Zara

Apr 21 '06 #3
Thank you Moonlit but still a bit fuzzy,
T gets replaced with the template class so for instance

class MyClass
{
public:
MyClass();
};

Vec<MyClass> Vector( 5 );
After this we have created a vector including 5 elements which are
class objects.
would make a call to MyClass() for the temporay second parameter
It would have been the same as you would have specified:

Vec<MyClass> Vector( 5, MyClass() );
In this part you initialize that vector elements(objects actually) with
the MyClass() function this is the part I did not understand.
--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"utab" <um********@gmail.com> wrote in message
news:11*********************@z34g2000cwc.googlegro ups.com...
Dear all,

Sorry for the first mail, I pressed enter by mistake.

On page 190,

template <class T> class Vec{

public:
Vec() {create();}
explicit Vec(size_type n, const T& val=T()){create(n,val)}

.
.
.

Could anyone clarify the default argument for me

const T& val=T()

T is the type, but I could not understand T(), I went bak to function
templates and checked if I missed sth. but could not find.

The explanation on the next page is
"Note that it uses a default argument (§7.3/127) for the second
parameter. Thus, the constructor effectively defines two constructors:
One takes a single argument of type size_type; the other takes a
size_type and a const T&. In both cases we call a version of create
that takes a size and a value. We'll assume that this function, which
we'll write in §11.5/203, will allocate enough memory to hold n
objects of type T, and will give those elements the initial value
specified by val."

Many thx.


Apr 21 '06 #4
> T() is an element constructed with a constructor that takes no
parameters, or a constructor thathas default values for all
parameters. If there is no such constructor, the compiler will issue
an error.

For integral types, it is equivalent to assigning it a 0.

regards,

Zara


T is a type name, lets say I gave it an integral type int then OK it is
value-initilaized to 0. But when I gave a class object the
initialization depends on the constructors of that object. If an
appropriate constructor is applied or not. The thing I am confused is
the function notation after the type T: T()

Thx and regards

Apr 21 '06 #5
Hi,

There is really no magic there. It just constructs the object (in my example
MyClass) and then assigns it to val. The same syntax could appear anywhere
in your program, consider

MyClass MyVariable = MyClass(); // Constructs a temporary object MyClass and
assigns it to MyVariable
--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"utab" <um********@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
Thank you Moonlit but still a bit fuzzy,
T gets replaced with the template class so for instance

class MyClass
{
public:
MyClass();
};

Vec<MyClass> Vector( 5 );
After this we have created a vector including 5 elements which are
class objects.
would make a call to MyClass() for the temporay second parameter
It would have been the same as you would have specified:

Vec<MyClass> Vector( 5, MyClass() );
In this part you initialize that vector elements(objects actually) with
the MyClass() function this is the part I did not understand.
--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"utab" <um********@gmail.com> wrote in message
news:11*********************@z34g2000cwc.googlegro ups.com...
Dear all,

Sorry for the first mail, I pressed enter by mistake.

On page 190,

template <class T> class Vec{

public:
Vec() {create();}
explicit Vec(size_type n, const T& val=T()){create(n,val)}

.
.
.

Could anyone clarify the default argument for me

const T& val=T()

T is the type, but I could not understand T(), I went bak to function
templates and checked if I missed sth. but could not find.

The explanation on the next page is
"Note that it uses a default argument (§7.3/127) for the second
parameter. Thus, the constructor effectively defines two constructors:
One takes a single argument of type size_type; the other takes a
size_type and a const T&. In both cases we call a version of create
that takes a size and a value. We'll assume that this function, which
we'll write in §11.5/203, will allocate enough memory to hold n
objects of type T, and will give those elements the initial value
specified by val."

Many thx.

Apr 21 '06 #6
utab wrote:
Dear all,

Sorry for the first mail, I pressed enter by mistake.

On page 190,

template <class T> class Vec{

public:
Vec() {create();}
explicit Vec(size_type n, const T& val=T()){create(n,val)}

.
.
.

Could anyone clarify the default argument for me

const T& val=T()

T is the type, but I could not understand T(), I went bak to function
templates and checked if I missed sth. but could not find.
When you don't pass the second parameter, a temporary is constructed for
passing the second parameter, calling it's default constructor (T must
have a default constructor).
The explanation on the next page is
"Note that it uses a default argument (§7.3/127) for the second
parameter. Thus, the constructor effectively defines two constructors:
One takes a single argument of type size_type; the other takes a
size_type and a const T&. In both cases we call a version of create
that takes a size and a value. We'll assume that this function, which
we'll write in §11.5/203, will allocate enough memory to hold n
objects of type T, and will give those elements the initial value
specified by val."

Many thx.

Apr 21 '06 #7

Thank you Moonlit,

Moonlit wrote:
Hi,

There is really no magic there. It just constructs the object (in my example
MyClass) and then assigns it to val. The same syntax could appear anywhere
in your program, consider
It is related with templates I guess: so If the type T is a class
object then it looks for the appropriate constructor to construct the
object, right?

But if the type is a built-in type such as an int then is not this
syntax weird( T() )? Or I am still missing some inportant pieces about
constructors and the way they work?
MyClass MyVariable = MyClass(); // Constructs a temporary object MyClass and
assigns it to MyVariable


This is quite clear with me
Thx...

Apr 21 '06 #8
* utab:
T() is an element constructed with a constructor that takes no
parameters, or a constructor thathas default values for all
parameters. If there is no such constructor, the compiler will issue
an error.

For integral types, it is equivalent to assigning it a 0.

regards,

Zara


T is a type name, lets say I gave it an integral type int then OK it is
value-initilaized to 0. But when I gave a class object the
initialization depends on the constructors of that object. If an
appropriate constructor is applied or not. The thing I am confused is
the function notation after the type T: T()


Bringing back some context, the question is about the formal argument

const T& val=T()

"const T& val" says it its name is "val" and that it is a reference to a
constant T. This could also be written as "T const& val". I prefer
that form because it generalizes, while the first is a special case.

Now, about the "=" and following.

C++ allows you to initialize a reference to constant, with a an ordinary
value-producing expression.

"=T()" is a C++ copy initialization syntax, the same syntax as in

int const nOranges = 1234;

"=T()" provides a default value for this argument. The default value is
"T()". Which is a temporary, default-constructed T object.


--
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?
Apr 21 '06 #9
Thank you, This is not a question just a comment about the book.

This book is nice I learn many things by reading it. But sometimes that
gives a lot in a relatively short description which you can not
understand the whole contents.

But still it is fine...

THX...
Alf P. Steinbach wrote:
* utab:
T() is an element constructed with a constructor that takes no
parameters, or a constructor thathas default values for all
parameters. If there is no such constructor, the compiler will issue
an error.

For integral types, it is equivalent to assigning it a 0.

regards,

Zara


T is a type name, lets say I gave it an integral type int then OK it is
value-initilaized to 0. But when I gave a class object the
initialization depends on the constructors of that object. If an
appropriate constructor is applied or not. The thing I am confused is
the function notation after the type T: T()


Bringing back some context, the question is about the formal argument

const T& val=T()

"const T& val" says it its name is "val" and that it is a reference to a
constant T. This could also be written as "T const& val". I prefer
that form because it generalizes, while the first is a special case.

Now, about the "=" and following.

C++ allows you to initialize a reference to constant, with a an ordinary
value-producing expression.

"=T()" is a C++ copy initialization syntax, the same syntax as in

int const nOranges = 1234;

"=T()" provides a default value for this argument. The default value is
"T()". Which is a temporary, default-constructed T object.


--
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?


Apr 21 '06 #10
utab wrote:
Thank you Moonlit,

Moonlit wrote:
Hi,

There is really no magic there. It just constructs the object (in my example
MyClass) and then assigns it to val. The same syntax could appear anywhere
in your program, consider

It is related with templates I guess: so If the type T is a class
object then it looks for the appropriate constructor to construct the
object, right?

But if the type is a built-in type such as an int then is not this
syntax weird( T() )? Or I am still missing some inportant pieces about
constructors and the way they work?


Why?
int() expresion is valid

MyClass MyVariable = MyClass(); // Constructs a temporary object MyClass and
assigns it to MyVariable

This is quite clear with me
Thx...

Apr 21 '06 #11
Yes I realized that now, took some time although :-))

Related with the construction of all the objects that you would like to
construct, is it a way to emphasize that(or a way :T())

I will try it with T only and try to understand by looking at simpler
examples on construction, but thank you for the replies.

Regards

Carlos Martinez wrote:
utab wrote:
Thank you Moonlit,

Moonlit wrote:
Hi,

There is really no magic there. It just constructs the object (in my example
MyClass) and then assigns it to val. The same syntax could appear anywhere
in your program, consider

It is related with templates I guess: so If the type T is a class
object then it looks for the appropriate constructor to construct the
object, right?

But if the type is a built-in type such as an int then is not this
syntax weird( T() )? Or I am still missing some inportant pieces about
constructors and the way they work?


Why?
int() expresion is valid

MyClass MyVariable = MyClass(); // Constructs a temporary object MyClass and
assigns it to MyVariable

This is quite clear with me
Thx...


Apr 21 '06 #12

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

Similar topics

3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
isladogs
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...
0
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...

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.