| View previous topic :: View next topic |
| Author |
Message |
Wouter Genuit Guest
|
Posted: Wed Sep 24, 2003 11:42 am Post subject: forward declaration and typedef |
|
|
Hi,
I'm just wondering why the following code is not allowed. What is the
reasoning behind the error given by the compiler:
Comeau: invalid redeclaration of typename "A"
MS: redefinition; different basic types
class A;
class B
{
};
typedef B A;
int main()
{
return 0;
}
Thanks
Wouter
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Attila Feher Guest
|
Posted: Wed Sep 24, 2003 10:29 pm Post subject: Re: forward declaration and typedef |
|
|
Wouter Genuit wrote:
| Quote: | Hi,
I'm just wondering why the following code is not allowed. What is the
reasoning behind the error given by the compiler:
Comeau: invalid redeclaration of typename "A"
MS: redefinition; different basic types
class A;
|
It says: I will give you later a class-type, called A.
It says: Hi, I am class B, short for barebone. ;-)
It says: I say A is a name, which is a type-alias for a class-type called B.
Compiler says: Khm. Could you make up your mind please? Is A then a
class-type or an alias (just a new name) for a class type?
--
Attila aka WW
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dimitris Kamenopoulos Guest
|
Posted: Wed Sep 24, 2003 10:32 pm Post subject: Re: forward declaration and typedef |
|
|
Wouter Genuit wrote:
| Quote: | Hi,
I'm just wondering why the following code is not allowed.
class A;
class B
{
};
typedef B A;
int main()
{
return 0;
}
|
To me it makes sense. First you forward declare A, meaning it is a full
fledged class, and then you use its name as an alias for another class (B).
The fact that A is not yet completely defined is irrelevant. It is a class
name, period. What you are doing is something like:
typedef B int;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David B. Held Guest
|
Posted: Wed Sep 24, 2003 10:38 pm Post subject: Re: forward declaration and typedef |
|
|
"Wouter Genuit" <wgenuit (AT) NOSPAMbaanNOSPAM (DOT) com> wrote
Here you are declaring a unique type 'A'.
| Quote: | class B
{
};
typedef B A;
[...]
|
Here you are declaring an alias for 'B'. I guess that means you
can't forward-declare typedefs. ;)
Dave
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Thu Sep 25, 2003 1:29 pm Post subject: Re: forward declaration and typedef |
|
|
"Wouter Genuit" <wgenuit (AT) NOSPAMbaanNOSPAM (DOT) com> wrote
| Quote: | class A;
class B
{
};
typedef B A;
|
I think the reason this is an error is because the sizeof(A*) may be
defferent from sizeof(B*). If A and B are class types, then they have the
same sizeof. But fundamental types may have a different sizeof. So
class A;
struct C { A * a; };
// thus sizeof(C) == sizeof(A*)
typedef B char;
// now sizeof(C) == sizeof(char*), which may be < sizeof(A*)
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael Jørgensen Guest
|
Posted: Thu Sep 25, 2003 1:36 pm Post subject: Re: forward declaration and typedef |
|
|
"David B. Held" <dheld (AT) codelogicconsulting (DOT) com> wrote
[snip]
| Quote: | I guess that means you
can't forward-declare typedefs.
|
Well, you could easily achieve this in the following way:
class B;
typedef B A;
/* later... */
class B
{
};
-Michael.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Thu Sep 25, 2003 11:40 pm Post subject: Re: forward declaration and typedef |
|
|
[email]wgenuit (AT) NOSPAMbaanNOSPAM (DOT) com[/email] (Wouter Genuit) wrote (abridged):
| Quote: | I'm just wondering why the following code is not allowed.
class A;
class B
{
};
|
Let's insert some code at this point:
int func( A &a ) { return 1; }
int func( B &b ) { return 2; }
All OK so far; different functions with different signatures.
This would retrospectively give the functions the same signature,
meaning one of them must be wrong. The problem is that "class A"
says A is a new type, and the typedef is making it an alias for
an existing type. They are doing incompatible things.
-- Dave Harris, Nottingham, UK
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|