Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 17th, 2006, 04:15 AM
Belebele
Guest
 
Posts: n/a
Default class declaration and definition inside a cpp file

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::operator() 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::operator() outside of the class,
the intended calls are made.

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

Thanks

  #2  
Old October 17th, 2006, 04:55 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: class declaration and definition inside a cpp file

* Belebele:
Quote:
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?).

Quote:
/* ... */
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::operator() 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.


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  #3  
Old October 17th, 2006, 10:45 AM
Martin Steen
Guest
 
Posts: n/a
Default Re: class declaration and definition inside a cpp file

Belebele wrote:
Quote:
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
  #4  
Old October 17th, 2006, 12:55 PM
Belebele
Guest
 
Posts: n/a
Default Re: class declaration and definition inside a cpp file

Quote:
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)
Quote:
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 ...)

  #5  
Old October 17th, 2006, 02:25 PM
Gavin Deane
Guest
 
Posts: n/a
Default Re: class declaration and definition inside a cpp file


Belebele wrote:
Quote:
Quote:
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)
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.
Quote:
Quote:
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 ...)
Have you looked at whether anonymous namespaces will solve your
problem?

Gavin Deane

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles