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

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

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


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

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

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

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

P: n/a
If a statement can be either parsed as function declaration or object
instantiation, the compiler will prefer the former.

ben
Jul 23 '05 #6

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

P: n/a
wij
Jonathan Mcdougall wrote:
...
http://www.gotw.ca/gotw/075.ht m


Thanks for the link, it explains well.

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.