473,386 Members | 1,793 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.

does `C o1(C());' declare a function

Why doesn't line# 21 create an object of type `C' class? I think the
default construction function invocation `C()' creates a temporary
nameless object, and `o1' will be built with the copy construction
function of the same class from this temporary object. With g++ (GCC)
3.4.2 (mingw-special), line 21 doesn't create `o1' as type of `C'
class. No default and copy constructions are called for this line. I
don't understand this.

For line 22, only C::C(int) is called, no copy construction is called.
Could you please explain this also?

#include <iostream>

class C
{
public:
C(){
std::cout << "C()" << std::endl;
}

C(int ){
std::cout << "C(int)" << std::endl;
}

C(const C &){
std::cout << "C(const C &)" << std::endl;
}
};

int main()
{
C o1(C()); /*line 21*/
C o2(C(0)); /*line 22*/
C o3(o2);
}

Jan 18 '07 #1
6 1405
lovecreatesbea...@gmail.com wrote:
Why doesn't line# 21 create an object of type `C' class?
Yes.
I think the
default construction function invocation `C()' creates a temporary
nameless object, and `o1' will be built with the copy construction
function of the same class from this temporary object.
No.
With g++ (GCC)
3.4.2 (mingw-special), line 21 doesn't create `o1' as type of `C'
class. No default and copy constructions are called for this line. I
don't understand this.
Simple, somewhere in the ISO C++ standard they say if some declaration ca be
interpreted as a function declaration then it is a function declaration.

This combined with the (not very well known) fact that functions, besides
declaring them as you usually do: int func(float obj); you can also do it
with the following syntax (they are equivalent) int func(float(obj));
Because of this and the rule above the explanation is simple. In order to
work arround it just wrap with another set of paranthesis the object
constructor parameter like: C o1((C()));

--
Dizzy
http://dizzy.roedu.net

Jan 18 '07 #2
lovecreatesbea...@gmail.com wrote:
Why doesn't line# 21 create an object of type `C' class? I think the
default construction function invocation `C()' creates a temporary
nameless object, and `o1' will be built with the copy construction
function of the same class from this temporary object. With g++ (GCC)
3.4.2 (mingw-special), line 21 doesn't create `o1' as type of `C'
class. No default and copy constructions are called for this line. I
don't understand this.
See <http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19>.
For line 22, only C::C(int) is called, no copy construction is called.
Could you please explain this also?
The compiler is free to optimize that away in an initialization.
#include <iostream>

class C
{
public:
C(){
std::cout << "C()" << std::endl;
}

C(int ){
std::cout << "C(int)" << std::endl;
}

C(const C &){
std::cout << "C(const C &)" << std::endl;
}
};

int main()
{
C o1(C()); /*line 21*/
C o2(C(0)); /*line 22*/
C o3(o2);
}
Cheers! --M

Jan 18 '07 #3
Dizzy wrote:
lovecreatesbea...@gmail.com wrote:
>Why doesn't line# 21 create an object of type `C' class?

Yes.
Love your answer. :-)
Jan 18 '07 #4
lovecreatesbea...@gmail.com wrote:
Why doesn't line# 21 create an object of type `C' class? I think the
default construction function invocation `C()' creates a temporary
nameless object, and `o1' will be built with the copy construction
function of the same class from this temporary object. With g++ (GCC)
3.4.2 (mingw-special), line 21 doesn't create `o1' as type of `C'
class. No default and copy constructions are called for this line. I
don't understand this.
info gcc,Invoking GCC, C++ Dialect Options

`-fno-elide-constructors'
The C++ standard allows an implementation to omit creating a
temporary which is only used to initialize another object of the
same type. Specifying this option disables that optimization, and
forces G++ to call the copy constructor in all cases.

Jan 19 '07 #5

"Dizzy" <di***@roedu.netwrote in message
news:45***********************@news.sunsite.dk...
lovecreatesbea...@gmail.com wrote:
>With g++ (GCC)
3.4.2 (mingw-special), line 21 doesn't create `o1' as type of `C'
class. No default and copy constructions are called for this line. I
don't understand this.

Simple, somewhere in the ISO C++ standard they say if some declaration ca
be
interpreted as a function declaration then it is a function declaration.

This combined with the (not very well known) fact that functions, besides
declaring them as you usually do: int func(float obj); you can also do it
with the following syntax (they are equivalent) int func(float(obj));
That is true, but that is not why it's a function declaration _in this
particular case_. It would be the case if
C o1(C(o2));
was used, but the function parameter name (o2 in the above example) was
omitted, making it a function declaration that returns C, and accepts a
[function that returns C with no parameters] as a parameter.

- Sylvester
Jan 19 '07 #6
On Fri, 19 Jan 2007 19:34:29 +0800, Sylvester Hesp <s.****@oisyn.nlwrote:

int func(int x)=int func(int),so
int func(int())=int func(int func1(void))
....

--
Hello,World!
----legolaskiss.
Jan 19 '07 #7

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

Similar topics

1
by: lawrence | last post by:
I'm trying to read up on the rfc's that govern form inputs. Much of what I'm reading is stuff I didn't know before and some of it is alarming. This one left with me questions: ...
6
by: greenflame | last post by:
I have been working for some time on a script that will show a matrix with the elements aligned on the right for sometime and finally got it to work. Then I did some patching up and ran the script...
2
by: Lance Geeck | last post by:
I have many items that I lifted off from Microsoft's website several years ago. These samples were in VB6. I now want to convert an application to VB.NET. I am getting an error that says "As Any...
0
by: Benjamin Lukner | last post by:
Hi! I'd like to dynamically call API functions from different DLLs, depending on what platform the program runs. Now I'm wondering what the most simple way may be (without meta code and compiler...
3
by: Altman | last post by:
I am having problems with Declaring a function from a dll. I had it work in VB 6 but I can't get it to work in VB.net. This was the call in VB6 Public Declare Function MBTConnect Lib "MBT" (ByVal...
2
by: Tany | last post by:
How can I declare function returning array of Integer pointers . Please help !!
5
by: benben | last post by:
How do you declare a function f that takes a parameter that is a pointer to itself? So you can do f(f); Pardon my curiosity! Ben
4
by: marss | last post by:
How to declare function that accepts any generic list as input parameter? I mean I need void foo(List<list) { } where: Valid parameters are List<SomeInfo>, List<string>, List<Guid>, e.t.c....
4
by: dolphin | last post by:
Hi All I read a .cpp files,find that static void fun(void){......} int main() { .......... } What does this static function mean?Is it the same as the static
2
by: zzzxtreme | last post by:
hi i built a very simple dll with delphi library Printing; uses SysUtils, Classes, Dialogs; {$R *.res} function Hello: WideString; stdcall; begin ShowMessage('test'); result := 'blah';
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:
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.