Page 1 of 1

Too stupid for C++

Posted: Sat Aug 02, 2008 1:30 am
by Jonathan
I have a working project with a forward declaration in a header file like

Code: Select all

typedef class Eventtype Eventtype;
And then the actual declaration is in some .cc file along with the definition. And then there's code that includes this header and somehow makes use of the class. How is this possible? I am trying to create a new project linking to the same type and I get an error in my project about not having a definition for the forward declaration.

I did

grep -r Eventtype | grep class *

But I didn't find anything. I feel like I am losing my mind.

The reason for the weirdness has to to with the class containing a member which is a pointer to another instance of the class. At one point the declaration was in the header file, but someone moved it six years ago.

Re: Too stupid for C++

Posted: Sat Aug 02, 2008 1:54 am
by George
I am somewhat surprised that the typedef is legal, since I thought shadowing was only for names with different scopes. I wonder if that line supported some crappy, pre-standard compiler that didn't obey C++'s rule that user defined types are treated like built in types.

You can use pointers or references to a forward declared type without having the definition available. You can't use their members (data or methods), but you can store and pass the pointers. So it's possible that the existing code that successfully uses the class with only the header's forward declaration is just manipulating pointers to it without ever trying to access members. On the other hand, if your code tries to access members, construct, destruct, declare an array of, instantiate a template using--or a few other things I can't think of--the class, then it would require the class definition to be visible in the compilation unit or it would fail to compile. I can't think of any scenario where it would compile with just a forward declaration but not link.

I don't understand the rationale you provided, unless it again dates to a pre-standard compiler. This should be legal without a forward declaration:

Code: Select all

class Foo
{
  Foo *otherFoo; // Foo & otherFoo; would also be legal, but Foo foo; would not be.  
  void doFoo(Foo *);
};

Re: Too stupid for C++

Posted: Sat Aug 02, 2008 7:58 am
by quantus
Agreed, that typedef is not necessary assuming it is legal. Anyways, is a typedef a forward declaration? I thought to declare that the Eventtype class exists without defining what it is, you would just want:

Code: Select all

class Eventtype;
As George said, that's not even necessary though if you're referencing another instance of the same class, like in a linked list or tree because you've already declared the class and are on to the definition portion. If you want to use member functions, you need to make sure they're declared already as well (probably in the .h file!). This is another area where Java seems to handle things more cleanly by not needing silly .h files to help the compiler figure things out.
get an error in my project about not having a definition for the forward declaration
This makes me think that you're just not compiling/linking in the .cc file for Eventtype...
grep -r Eventtype | grep class *
Also, did you mean:

grep -r Eventtype ./ | grep class

??

Re: Too stupid for C++

Posted: Sat Aug 02, 2008 4:52 pm
by Jonathan
grep -r Eventtype * | egrep class