C
CoolPint
I read that there has to be only one definition of classes, functions,
etc. in a project.
I just realized I might have broken the rule by defining the exception
classes in the header file. But my linker does NOT complain. For
example,
Let's say I have a header file somewhat like below
// MyClass.h
class MyClassException1 {};
class MyClassException2 {};
class MyClass {
public:
// blah blah blah
private:
// blah blah blah
};
and a corresponding translate unit MyClass.cpp containing definitions
of member functions.
If I have a project with many translation units all including
"MyClass.h", wouldn't this be breaking the one definition rule since
each unit would be defining MyClassException1 and MyClassException2
classes again and again?
Why isn't my linker saying anything?
In a situation like this, should I have a separate translation unit
and the header file for the exception classes to make sure I don't
break ODR?
// MyClassException.h
class MyClassException1;
class MyClassException2;
// MyClassException.cpp
class MyClassException1 { };
class MyClassException2 { };
To be honest, I feel rather reluctant to create another set of a
header and translation unit for simple things like above. But if it's
required to make sure I don't break ODR, then I have no choice, do I?
Please help understand better. Thanks in Advance!
etc. in a project.
I just realized I might have broken the rule by defining the exception
classes in the header file. But my linker does NOT complain. For
example,
Let's say I have a header file somewhat like below
// MyClass.h
class MyClassException1 {};
class MyClassException2 {};
class MyClass {
public:
// blah blah blah
private:
// blah blah blah
};
and a corresponding translate unit MyClass.cpp containing definitions
of member functions.
If I have a project with many translation units all including
"MyClass.h", wouldn't this be breaking the one definition rule since
each unit would be defining MyClassException1 and MyClassException2
classes again and again?
Why isn't my linker saying anything?
In a situation like this, should I have a separate translation unit
and the header file for the exception classes to make sure I don't
break ODR?
// MyClassException.h
class MyClassException1;
class MyClassException2;
// MyClassException.cpp
class MyClassException1 { };
class MyClassException2 { };
To be honest, I feel rather reluctant to create another set of a
header and translation unit for simple things like above. But if it's
required to make sure I don't break ODR, then I have no choice, do I?
Please help understand better. Thanks in Advance!