inline Konstruktor

Unicate

Erfahrenes Mitglied
Ist es möglich mit dem gcc einen inline Konstruktor zu forcen?

Von Natur aus macht der das nicht.

Idee ist folgende:
Code:
inline Exception::Exception(int code, char *message) throw() {
	this->code = code;
	this->message = message;
	this->line = __LINE__;
	this->filename = __FILE__;
}

Und hier noch der aufruf dazu:

Code:
void test() {
	throw Exception(1 , "Testmessage");
}
Ausgabe des compilers:
Code:
.... /Debug/../src/inline.cpp:13: undefined reference to `Exception::Exception(int, char*)'

Jemand eine Idee oder vielleicht schon eine funktionierende Variante?

Mitschicken der informationen(file,line) wäre eine variante, aber mit der geb ich mich mal noch nicht zu frieden...
 
Hallo Unicate,

schau mal in die C++ FAQ, vor allem Punkt 9.7. Dein Vorhaben wird allerdings so und so scheitern, da __LINE__ und __FILE__ bereits vom Präprozessor ersetzt werden, das Inlinen von Methoden aber erst beim Kompilieren geschieht. Mögliche Lösung: übergib deiner Exception-Klasse die Informationen über den Konstruktor und erstelle ein Präprozessor-Makro der Art:
C++:
#define EXCEPTION(code, message) Exception(code, message, __LINE__, __FILE__)

Grüße,
Matthias
 
SUPER Danke Matthias!

Hier die Klasse für die Öffentlichkeit, ist zwar nix besonderes weiter, aber für die meisten zwecke reichts warscheinlich:

Exception.h:
Code:
#ifndef EXCEPTION_H_
#define EXCEPTION_H_
/**
 * Exception to throw
 *
 *
 * @author unicate(unicate@hackschweine.de)
 *
 *
 * @date 03.05.2010
 *
 */



class Exception {
protected:
	int code;
	char *message;
	char *filename;
	unsigned int line;
public:
	/**
	 * Constructor receives an error code and a message
	 *
	 * @param code error code
	 * @param message message to write
	 * @param file name of the file where the Exception is thrown
	 * @param line line where the Exception is thrown
	 *
	 * @throw nothing is not allowed to throw anything
	 */
	Exception(int code, char *message, char *file, unsigned int line) throw() {
		this->code = code;
		this->message = message;
		this->line = line;
		this->filename = file;
	}
	/**
	 * destructor doesn't have to do anything
	 */
	~Exception(){};

	/**
	 * @return error code of the current exception
	 */
	int getCode();
	/**
	 * @return returns the message for this exception
	 */
	char *getMessage();
	/**
	 * @return file name where the exception is thrown
	 */
	char *getFilename();

	/**
	 * @return line number where the exception is thrown
	 */
	unsigned int getLinenumber();

};
#define Exception(code, message) Exception(code,message,__FILE__,__LINE__)


#endif /* EXCEPTION_H_ */

und die cpp:
Code:
#include "Exception.h"


int Exception::getCode() {
    return code;
}

char *Exception::getMessage() {
    return message;
}

char *Exception::getFilename() {
	return this->filename;
}

unsigned int Exception::getLinenumber() {
	return this->line;
}
 
Hi.

message und filename (und der Rückgabetyp der Getter) sollten const char* sein, oder übergibst du dort wirklich änderbare Strings? (sieht jedenfalls nicht so aus) Und die Getter Methoden sollten ebenfalls const sein. (=> const correctness)

Und warum implementierst du nicht std::exception? Das macht sich meist besser wenn man an einer Stelle alle Exceptions behandeln (und sie dann z.B. weiterwerfen) möchte.

Gruß
 
Zurück