constructor call syntax

T

toton

Hi,
Which one is the correct syntax for constructor for static memory
allocation

Object obj("param");
or
Object obj = Object("param");

1) For the first one, how compiler checks it as not a function
deceleration rather than ctor call?
2) For the second one, is the assignment op is also called? If so, how
to remove such assignement call? (like, object initialization with new,
which do not need an assignment operator)
3) If I do not declare explicit in the ctor, will it convert all
premitive type to the matching ctor?
e.g Object obj = "hello"; will it convert "hello" to a char* matching
ctor if found? Is it true for multiple arguments also?

thanks
 
R

Rolf Magnus

toton said:
Hi,
Which one is the correct syntax for constructor for static memory
allocation

Object obj("param");
or
Object obj = Object("param");

Both are correct.
1) For the first one, how compiler checks it as not a function
deceleration rather than ctor call?

The string literal "param" cannot be parsed as a type, so it can't be a
function declaration.
2) For the second one, is the assignment op is also called?

It's not assignment, it's copy construction.
Just remember those patterns:

type name = value; <- copy construction
name = value; <- assignment
If so, how to remove such assignement call? (like, object initialization
with new, which do not need an assignment operator)

Many compilers can optimize the copy construction away, and they are
explicitly allowed to. There is no way to ensure the copy construction is
not done.
3) If I do not declare explicit in the ctor, will it convert all
premitive type to the matching ctor?

Not sure what exactly you mean here. If a constructor that can take one
argument is declared as 'explicit', it won't be used for implicit
conversions.
e.g Object obj = "hello"; will it convert "hello" to a char* matching
ctor if found?

Yes, if that constructor is not declared 'explicit'.
Is it true for multiple arguments also?

'explicit' has no effect on multiple arguments, since that wouldn't make
sense. Or would you expect something like:

Object obj = "hello", 3;

to call Object::OBject(const char*, int)?
 
P

pp

toton said:
Hi,
Which one is the correct syntax for constructor for static memory
allocation

Object obj("param");
or
Object obj = Object("param");

1) For the first one, how compiler checks it as not a function
deceleration rather than ctor call?
2) For the second one, is the assignment op is also called? If so, how
to remove such assignement call? (like, object initialization with new,
which do not need an assignment operator)
3) If I do not declare explicit in the ctor, will it convert all
premitive type to the matching ctor?
e.g Object obj = "hello"; will it convert "hello" to a char* matching
ctor if found? Is it true for multiple arguments also?

thanks

both the syntaxes will do the same thing.
ans 1) i think here value is passed in the brackets so its not a
function declaration.
ans 2) assignment op is not called here. the same constructor will be
called.
ans 3) yes that is called implicit conversion. no its not ture for
multiple args, only for single arg constructors.

I think this will hep you.
 
M

Michiel.Salters

toton said:
Hi,
Which one is the correct syntax for constructor for static memory
allocation

Object obj("param");
or
Object obj = Object("param");

Both are OK, assuming Object can indeed be copied.
1) For the first one, how compiler checks it as not a function
deceleration rather than ctor call?

"param" is an object with type char[6]. Function declarations cannot
contain
expressions.
2) For the second one, is the assignment op is also called? I
No, although the copy constructor might.
3) If I do not declare explicit in the ctor, will it convert all
premitive type to the matching ctor?

Not just primitive types - any type for which a matching non-explicit
constructor exists can be converted.
e.g Object obj = "hello"; will it convert "hello" to a char* matching
ctor if found? Is it true for multiple arguments also?

Yes, it could convert "hello" (char const[6]) to char*, const char*,
const char[].
Of course, since operator= has only one right-hand-side, only
constructors are
considered that can be called with one argument. (including those with
a default
second argument)
 
H

Heinz Ozwirk

toton said:
Hi,
Which one is the correct syntax for constructor for static memory
allocation

Object obj("param");
or
Object obj = Object("param");

Both are correct.
1) For the first one, how compiler checks it as not a function
deceleration rather than ctor call?

If it looks like a function decleration, it is a function declaration. If it
cannot be a function declaration, it is a variable definition with
initialization. (There is no such thing as a "constructor call"). Be
carefull if a cast ist required in an initialization. Something like

int foo((int) bar);

is a valid function declaration in C (with some extra parenthesises), so it
also is a function declaration in C++.
2) For the second one, is the assignment op is also called? If so, how
to remove such assignement call? (like, object initialization with new,
which do not need an assignment operator)

No. By the rules, the compiler should first use a suitable constructor to
create a temporary Object, and it should then use the copy constructor to
initializie obj. But the compiler is alowed to optimize that initialization
and construct obj in-place. However, even if the copy constructor is not
called, it must be accessable in the context of that definition.
3) If I do not declare explicit in the ctor, will it convert all
premitive type to the matching ctor?
e.g Object obj = "hello"; will it convert "hello" to a char* matching
ctor if found? Is it true for multiple arguments also?

Yes.

HTH
Heinz
 
T

toton

Both are OK, assuming Object can indeed be copied.
Thus, both cases are equivalent and calls copy ctor, or only the second
one do so?
Again, if I have a pointer to data, default copy ctor will not copy the
data itself? Or I need
to define a copy ctor explicitely in that case.
eg,
class Object{
private:
int* data;
int _size;
public:
Object(int size) : _size(size){
data = new int[_size];
}
~Object(){
delete[] _data;
}
};
Moreover, if I pass a reference to another object in the ctor, the
default copy ctor is not generated (as reference is not assignable) .
do I need to write a copy ctor in that case?
eg
if ctor is defined like this,
class Object{
private:
Object1& _obj1
public:
Object(Object1& obj1) : _obj1(obj);
};
Finally, if the compiler does not remove the copy ctor call actually,
will it cause a performance problem for large objects (like an Image
object)? In that case, will it be better to have object initialization
with new & smart_ptr, as it will not cann a copy ctor?
1) For the first one, how compiler checks it as not a function
deceleration rather than ctor call?

"param" is an object with type char[6]. Function declarations cannot
contain
expressions.
How to make the difference if the ctor is a no argument one,
i.e like Object obj(); ?
2) For the second one, is the assignment op is also called? I
No, although the copy constructor might.
3) If I do not declare explicit in the ctor, will it convert all
premitive type to the matching ctor?

Not just primitive types - any type for which a matching non-explicit
constructor exists can be converted.
e.g Object obj = "hello"; will it convert "hello" to a char* matching
ctor if found? Is it true for multiple arguments also?

Yes, it could convert "hello" (char const[6]) to char*, const char*,
const char[].
Of course, since operator= has only one right-hand-side, only
constructors are
considered that can be called with one argument. (including those with
a default
second argument)
This point is now clear to me.
thanks to all for the help.
abir
 
G

Greg Comeau

'explicit' has no effect on multiple arguments, since that wouldn't make
sense. Or would you expect something like:

Object obj = "hello", 3;

to call Object::OBject(const char*, int)?

The additional args could be default argument'ed.
 
F

Frederick Gotham

toton posted:
Hi,
Which one is the correct syntax for constructor for static memory
allocation

Object obj("param");
or
Object obj = Object("param");


It's about time this made it into the FAQ.
 
J

Jerry Coffin

Hi,
Which one is the correct syntax for constructor for static memory
allocation

Object obj("param");
or
Object obj = Object("param");

These are both correct, but they do have minutely different meanings.
The first constructs obj, passing "param" to its ctor. The second
constructs a temporary, passing "param" to its ctor, then creates obj as
a copy of the temporary. The difference between the two can be visible
if (for example) you prevent copy construction of Object's:

class Object {
Object(Object const &);
std::string data;
public:
Object(std::string const &init) : data(init) {}
};

// This works:
Object obj("Param");

// But the compiler should reject this:
Object obj2 = Object("Param");
1) For the first one, how compiler checks it as not a function
deceleration rather than ctor call?

This has been called the "most puzzling parse in C++". Syntactically,
the two look like:

type name '(' type [name] ');'
and:
type name '(' value ');'

where the '[name]' means the name is optional. The former is given
higher priority though, so if you what you have between parentheses CAN
be interpreted as a type, it will be, and the result is a function
declaration. If and only if what's between parentheses can't be treated
as a type, the compiler will attempt to treat it as a value, and it'll
be an object with initialization.
2) For the second one, is the assignment op is also called? If so, how
to remove such assignement call? (like, object initialization with new,
which do not need an assignment operator)

No -- the second uses the copy ctor, but not the assignment operator.
3) If I do not declare explicit in the ctor, will it convert all
premitive type to the matching ctor?
e.g Object obj = "hello"; will it convert "hello" to a char* matching
ctor if found? Is it true for multiple arguments also?

An implicit conversion always takes a single input and using it creates
an object of the specified type. That means the constructor can only
require a single parameter -- but it can allow other parameters if they
have default values.
 
R

Rolf Magnus

toton said:
Thus, both cases are equivalent and calls copy ctor, or only the second
one do so?

Only the second one _might_ do so. It isn't required to.
Again, if I have a pointer to data, default copy ctor will not copy the
data itself? Or I need to define a copy ctor explicitely in that case.

There is no such thing as a "default copy constructor". You probably meant
the compiler-generated one. That constructor does a memberwise copy, so
each member variable of your object is copied. If your member is a pointer,
than a pointer (and only the pointer) is what gets copied.
eg,
class Object{
private:
int* data;
int _size;
public:
Object(int size) : _size(size){
data = new int[_size];
}
~Object(){
delete[] _data;
}
};

In this case, the compiler-generated copy constructor won't do what you
want, and neither will the assignment operator.
Moreover, if I pass a reference to another object in the ctor, the
default copy ctor is not generated (as reference is not assignable) .

That won't stop the compiler from generating a copy constructor, since the
compiler-generated copy constructor doesn't use assingment at all.
do I need to write a copy ctor in that case?
eg
if ctor is defined like this,
class Object{
private:
Object1& _obj1
public:
Object(Object1& obj1) : _obj1(obj);
};
No.


Finally, if the compiler does not remove the copy ctor call actually,
will it cause a performance problem for large objects (like an Image
object)?

It might, if copying the object takes a significant amount of time.
In that case, will it be better to have object initialization
with new & smart_ptr, as it will not cann a copy ctor?

Possibly. It depends on the circumstances. Note that new and smart pointers
also impose an overhead.
1) For the first one, how compiler checks it as not a function
deceleration rather than ctor call?

"param" is an object with type char[6]. Function declarations cannot
contain
expressions.
How to make the difference if the ctor is a no argument one,
i.e like Object obj(); ?

In this case, the compiler can't. This is indeed parsed as a declaration of
a function named 'obj' taking no parameters and returning an Object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top