• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

In c++ what does constructor return

you2

Diamond Member
If you have a simple program:

class a() {
public:
a() {};
void x() {};
};

int main() {
a();
return(0);
}

What exactly does a() return ? I know it is not an instance of a since a().x() is illegal.

Where would I find this in the specification (not sure of terminology)
 
The constructors return value is void. The new operator would return an instance of an object so (new a()).x() would do what you want. By the constructor and destructor are both defined as void.
 
Catch is taking the parameter and setting up a trap to handle an illegal instruction/execution within the context of the constructor or function a().
 
Originally posted by: you2
Then what datatype does :

try {
..
..
.
} catch (a()) {
...
}
catch ?

It doesn't, the catch block needs to declare a type. As in :

catch( a E) not catch (a())

Bill
 
Actually it does. This is legal code it compiles. A coworker figured out what it is catching - it is catching a pointer to a function that returns type a (ala

a x() {return a()};

try {
throw &x
} catch (a() ) {
...
}
---
Yea I know the answer now but not when i asked the question.
 
Originally posted by: you2
Actually it does. This is legal code it compiles. A coworker figured out what it is catching - it is catching a pointer to a function that returns type a (ala

a x() {return a()};

try {
throw &x
} catch (a() ) {
...
}
---
Yea I know the answer now but not when i asked the question.

Aww good point (and duh), didn't even think of function pointers when I looked at that.
Bill
 
Originally posted by: you2
Actually it does. This is legal code it compiles. A coworker figured out what it is catching - it is catching a pointer to a function that returns type a (ala

a x() {return a()};

try {
throw &x
} catch (a() ) {
...
}
---
Yea I know the answer now but not when i asked the question.

Interesting. This little program confirms your friend's analysis, though I am not sure syntactically why it works.

#include <iostream>

class a
{
};

a foo()
{
a b;
return b;
}

int main(int argc, char* argv[])
{
try
{
throw foo;
}
catch( a() )
{
std::cout << "caught it" << std::endl;
}
return 0;
}

Thing is, the type of a pointer to foo() should be a (*)(). There's probably some shortcut notation in play. In the catch clause above the compiler has to assume the token evaluates to a type, so a() can't be read as a function call, since functions can't return types. So it must be seen as a (), but why doesn't it require a (*)()?

Kudos to your friend for working it out. Pointers to functions were always a bear in C++.
 
Back
Top