Puzzle: make new compilers understand what g++ 2.95.3 compiled

G

Guest

When language changes make old code uncompilable, that's not what is
called protection of investment.

New compilers (g++ > 3.2.3) reject classes where methods throw the
class they belong to.
gcc 2.95.3 allowed it.

What are the minimal modification, that leave the usage of the class
sound and can be compiled by newer compilers.

Thank you


( compile the following code with s.th. like g++ -g -o Exception
Exception.cpp -lstlport_gcc )

#include "iostream"

class Exception {
public:
Exception(int);
void setErrNo(int i) throw(Exception);
int errNo;
};

Exception::Exception(int e) {
errNo=e;
}

void Exception::setErrNo(int i) throw(Exception) {
auto Exception methodException(2);
errNo=i;
throw(methodException);
};

int main(char argc, char *argv[], char *env[]) {
try {
auto Exception mainException(1);
mainException.setErrNo(42);
} catch (Exception caughtmainException) {
std::cout << "caught mainException:" << mainException.errNo <<
std::endl;
}
}
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

When language changes make old code uncompilable, that's not what is
called protection of investment.

New compilers (g++ > 3.2.3) reject classes where methods throw the
class they belong to.
gcc 2.95.3 allowed it.

What are the minimal modification, that leave the usage of the class
sound and can be compiled by newer compilers.

Thank you

( compile the following code with s.th. like g++ -g -o Exception
Exception.cpp -lstlport_gcc )

#include "iostream"

class Exception {
public:
Exception(int);
void setErrNo(int i) throw(Exception);
int errNo;

};

Exception::Exception(int e) {
errNo=e;

}

void Exception::setErrNo(int i) throw(Exception) {
auto Exception methodException(2);
errNo=i;
throw(methodException);

};

int main(char argc, char *argv[], char *env[]) {
try {
auto Exception mainException(1);
mainException.setErrNo(42);
} catch (Exception caughtmainException) {
std::cout << "caught mainException:" << mainException.errNo <<
std::endl;
}
}

mainException is no longer in scope when you try to print errNo, are
you sure you didn't mean

td::cout << "caught mainException:" << caughtmainException.errNo <<
std::endl;
 
G

Guest

You are absolutely right.
This should be corrected in the initial message.

But that is not the real problem.

Thank you for your attention
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top