473,503 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Expression B b(A()) seemed no effect?

wij
Hi:
I encountered an expression B b(A()) which seemd to have no runtime
code compiled (as in the following example). Can anybody explain
this to me? Thank you.

// Build: g++ t.cpp (GCC version 3.4.2)
//
// [mypc]$./a.out
// strange?
// [mypc]$
//
#include <iostream>

class A {
public:
A() { std::cerr << "A ";
throw (const char*)"A_throw"; };
};

class B {
public:
B(const A&) { std::cerr << "B(A) "; };
};

int main(void)
{
try {
B b(A()); // constructing b object, no effect?
// expecting: "A_throw"
throw (const char*)"strange?";
}
catch(const char* str) {
std::cerr << str << '\n';
}
catch(...) {
std::cerr << "unknown\n";
};

return(0);
};

Jul 23 '05 #1
7 1140
Hi

wi*@seed.net.tw wrote:
Hi:
I encountered an expression B b(A()) which seemd to have no runtime
code compiled (as in the following example). Can anybody explain
this to me? Thank you.


B b(A());
is the declaration of a function b returning an object of type B and taking
a function as parameter.

A some_func();
B b(A());
B my_b = b(some_func);
Markus
Jul 23 '05 #2
<wi*@seed.net.tw> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com
Hi:
I encountered an expression B b(A()) which seemd to have no runtime
code compiled (as in the following example). Can anybody explain
this to me? Thank you.

// Build: g++ t.cpp (GCC version 3.4.2)
//
// [mypc]$./a.out
// strange?
// [mypc]$
//
#include <iostream>

class A {
public:
A() { std::cerr << "A ";
throw (const char*)"A_throw"; };
};

class B {
public:
B(const A&) { std::cerr << "B(A) "; };
};

int main(void)
{
try {
B b(A()); // constructing b object, no effect?
// expecting: "A_throw"
throw (const char*)"strange?";
}
catch(const char* str) {
std::cerr << str << '\n';
}
catch(...) {
std::cerr << "unknown\n";
};

return(0);
};


Further to Markus's point, you can achieve your intended effect with:

B b = A();

--
John Carson
Jul 23 '05 #3
John Carson wrote:
[..]
Further to Markus's point, you can achieve your intended effect with:

B b = A();


Or

B b((A()));

V
Jul 23 '05 #4
wij
Victor Bazarov wrote:
John Carson wrote:
[..]
Further to Markus's point, you can achieve your intended effect with:
B b = A();

Or
B b((A()));


Thanks for the fixup, But what is the reason?
So I can use it in genreal cases.

Jul 23 '05 #5
If a statement can be either parsed as function declaration or object
instantiation, the compiler will prefer the former.

ben
Jul 23 '05 #6
wi*@seed.net.tw wrote:
Victor Bazarov wrote:
John Carson wrote:
[..]
Further to Markus's point, you can achieve your intended effect with:
B b = A();

Or
B b((A()));


Thanks for the fixup, But what is the reason?
So I can use it in genreal cases.


http://www.gotw.ca/gotw/075.htm
Jonathan

Jul 23 '05 #7
wij
Jonathan Mcdougall wrote:
...
http://www.gotw.ca/gotw/075.ht m


Thanks for the link, it explains well.

Jul 23 '05 #8

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

Similar topics

23
3196
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
1
1298
by: Steve Zatz | last post by:
Is '@' a special character in regular expressions? I am asking because I don't understand the following: >>> import re >>> s = ' @' >>> re.sub(r'\b@','*',s) ' @' >>> s = ' a' >>>...
1
1844
by: Gareth James via .NET 247 | last post by:
I have an expression that when run uses 100% cpu for over 1minute. I can change the expression so this does not happen, but couldsome one explain why this happens so that I don't do it again ...
7
7986
by: Gary Duncan | last post by:
Hi all, My first incursion into this group so apologies if the following question is misplaced. That is, I'm trying to find some free javascript which implements the "ken burns effect" on...
3
5665
by: Jason luo | last post by:
Hi all, In c99-standard page 52,there is a sentence about void,as below: If an expression of any other type is evaluated as a void expression, its value or designator is discarded. I don't...
6
10641
by: Aaron Smith | last post by:
Ok. I have a dataset that has multiple tables in it. In one of the child tables, I have a column that I added to the DataSet (Not in the DataSource). This column does not need to be stored in the...
5
8463
by: Mystilleef | last post by:
Hello, Is there a simple flag to set to allow overlapping matches for the findall() regular expression method? In other words, if a string contains five occurrences of the string pattern "cat",...
21
2372
by: Steven T. Hatton | last post by:
I'm trying to improve my formal understanding of C++. One significant part of that effort involves clarifying my understanding of the vocabulary used to describe the language. This is from the...
18
7928
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
0
7205
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
7287
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
7353
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...
1
7011
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
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4689
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...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.