C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

forward declaration and typedef

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Wouter Genuit
Guest





PostPosted: Wed Sep 24, 2003 11:42 am    Post subject: forward declaration and typedef Reply with 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;

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





PostPosted: Wed Sep 24, 2003 10:29 pm    Post subject: Re: forward declaration and typedef Reply with quote



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.

Quote:
class B
{
};

It says: Hi, I am class B, short for barebone. ;-)

Quote:
typedef B A;

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





PostPosted: Wed Sep 24, 2003 10:32 pm    Post subject: Re: forward declaration and typedef Reply with quote



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





PostPosted: Wed Sep 24, 2003 10:38 pm    Post subject: Re: forward declaration and typedef Reply with quote

"Wouter Genuit" <wgenuit (AT) NOSPAMbaanNOSPAM (DOT) com> wrote

Quote:
[...]
class A;

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





PostPosted: Thu Sep 25, 2003 1:29 pm    Post subject: Re: forward declaration and typedef Reply with quote

"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





PostPosted: Thu Sep 25, 2003 1:36 pm    Post subject: Re: forward declaration and typedef Reply with quote


"David B. Held" <dheld (AT) codelogicconsulting (DOT) com> wrote

[snip]
Quote:
I guess that means you
can't forward-declare typedefs. Wink

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





PostPosted: Thu Sep 25, 2003 11:40 pm    Post subject: Re: forward declaration and typedef Reply with quote

[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.


Quote:
typedef B A;

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.