class declaration and definition inside a cpp file

B

Belebele

In a cpp file, say a.cpp I have the following code:

#include <element.h>

struct Functor
{
void operator (Element const& ) const { /* ... */ }
};

/* ... */
collOfElements.forEachElement(Functor());

Then, on another cpp file, b.cpp I have

#include <element.h>

struct Functor
{
void operator (Element const& ) const { /* different
from the functor in a.cpp */ }
};

/* ... */
anotherCollOfElements.forEachElement(Functor());


The code that the linker produces calls Functor::eek:perator() defined in
a.cpp when the anotherCollOfElements (defined in b.cpp) is iterated
over. My intention is that the Functor structs, although they have the
same name, are different since they reside in different files and each
functor is called when the code in its cpp file invokes it.

Moreover, if I define b.cpp's Functor::eek:perator() outside of the class,
the intended calls are made.

I am confused as to what the linker is doing. Any idea?

Thanks
 
A

Alf P. Steinbach

* Belebele:
In a cpp file, say a.cpp I have the following code:

#include <element.h>

struct Functor
{
void operator (Element const& ) const { /* ... */ }
};

No, you don't have that code.

Have you tried compiling?

Please don't type in arbitrary pseudo-code when posting: copy and paste
real code (do you understand why?).

/* ... */
collOfElements.forEachElement(Functor());

Then, on another cpp file, b.cpp I have

#include <element.h>

struct Functor
{
void operator (Element const& ) const { /* different
from the functor in a.cpp */ }
};

/* ... */
anotherCollOfElements.forEachElement(Functor());


The code that the linker produces calls Functor::eek:perator() defined in
a.cpp when the anotherCollOfElements (defined in b.cpp) is iterated
over.

You have violated the One Definition Rule (the ODR): you can't have two
classes with the same name in the same namespace, here the global one.

Put the classes in different namespaces.

E.g. anonymous namespaces.
 
M

Martin Steen

Belebele said:
I am confused as to what the linker is doing. Any idea?

Yes.

1. use different names for different functions
2. put the declarations in header files

So you and the linker won't get confused ;)

-Martin
 
B

Belebele

1. use different names for different functions

So, no link-time support to indicate a class name collision? Have to
wait until run-time, when the app crashes to realize the little slip
(or worse, it may not crash at all, and produce undefined behavior)
2. put the declarations in header files

Seems excessive for classes only used in one cpp module to increase
encapsulation, to avoid duplication, to make the code easier to
understand (all that good stuff, you know ...)
 
G

Gavin Deane

Belebele said:
So, no link-time support to indicate a class name collision? Have to
wait until run-time, when the app crashes to realize the little slip
(or worse, it may not crash at all, and produce undefined behavior)

What support your linker might offer you is outside the scope of the
C++ language. Violating the one definition rule is undefined behaviour
as far as C++ is concerned.
Seems excessive for classes only used in one cpp module to increase
encapsulation, to avoid duplication, to make the code easier to
understand (all that good stuff, you know ...)

Have you looked at whether anonymous namespaces will solve your
problem?

Gavin Deane
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top