By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,441 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,441 IT Pros & Developers. It's quick & easy.

Accerated C++ question

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
* 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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.