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

constructor call syntax

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

Aug 22 '06 #1
9 1959
pp

toton wrote:
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.

Aug 22 '06 #2
toton wrote:
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)?

Aug 22 '06 #3
toton wrote:
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)

Aug 22 '06 #4
"toton" <ab*******@gmail.comschrieb im Newsbeitrag
news:11**********************@m73g2000cwd.googlegr oups.com...
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

Aug 22 '06 #5

Mi*************@tomtom.com wrote:
toton wrote:
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.
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

Aug 22 '06 #6
In article <ec*************@news.t-online.com>,
Rolf Magnus <ra******@t-online.dewrote:
>toton wrote:
>>...
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)?
The additional args could be default argument'ed.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 22 '06 #7
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.

--

Frederick Gotham
Aug 22 '06 #8
In article <11**********************@m73g2000cwd.googlegroups .com>,
ab*******@gmail.com says...
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 22 '06 #9
toton wrote:
>
Mi*************@tomtom.com wrote:
>toton wrote:
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.
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.

Aug 22 '06 #10

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

Similar topics

34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
24
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
6
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines...
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
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...
6
by: =?Utf-8?B?T2xkQnV0U3RpbGxMZWFybmluZw==?= | last post by:
I forget the right syntax.... If I Have multiple signatures for a constructor, but I want each to call a "main" version of the constructor...I can't remember how to do this in C#.... public...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
3
by: mhvaughn | last post by:
struct S1 { int i; }; struct S2 { S1 s; // version 1 S2() {} ; // version 2
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.