can forward declearation of struct as class cause any problem

N

nurxb01

Hi
I have a very basic doubt.
The application i'm working on has some old code already in place.
There are some struct data type decleared in a .h file and are being
used for RPC communication.
Further in the application code at some places forward declearion
for these datatypes are given as class .
The code compiles without any errors and warning on solaris (g++)and
linux (g++)but give Compiler Warning C4099 on windows (MSDEV VC6.0) but
get compiled without any erros.
---------Description of warning C4099 starts ( from
msdn)-----------------------------
Compiler Warning (level 2) C4099
'identifier' : type name first seen using 'objecttype1' now seen using
'objecttype2'

The specified object was declared as either a structure or a class but
was defined as a class or a structure, respectively.
-----------------------------Description ends
------------------------------------

I never observe any problem in the execution of the program on ( Win2k
, solraris 2.7, RH Linux 7.1 ).

But can the above mentioned code be cause any problem or it's
perfactly ok to use that code.

//To make it more clear below is the two example header files
//from file a.h
//
struct CompositePt
{
double Pt;
bool mode;
};
typedef struct CompositePt CompositePt;

//from file b.h
//
class CompositePt;

class SomeApp_Code {

int test_function(CompositePt& point );
};

Thanks in advance for help
 
M

mlimber

Hi
I have a very basic doubt.
The application i'm working on has some old code already in place.
There are some struct data type decleared in a .h file and are being
used for RPC communication.
Further in the application code at some places forward declearion
for these datatypes are given as class .
The code compiles without any errors and warning on solaris (g++)and
linux (g++)but give Compiler Warning C4099 on windows (MSDEV VC6.0) but
get compiled without any erros.
---------Description of warning C4099 starts ( from
msdn)-----------------------------
Compiler Warning (level 2) C4099
'identifier' : type name first seen using 'objecttype1' now seen using
'objecttype2'

The specified object was declared as either a structure or a class but
was defined as a class or a structure, respectively.
-----------------------------Description ends
------------------------------------

I never observe any problem in the execution of the program on ( Win2k
, solraris 2.7, RH Linux 7.1 ).

But can the above mentioned code be cause any problem or it's
perfactly ok to use that code.

//To make it more clear below is the two example header files
//from file a.h
//
struct CompositePt
{
double Pt;
bool mode;
};
typedef struct CompositePt CompositePt;

//from file b.h
//
class CompositePt;

class SomeApp_Code {

int test_function(CompositePt& point );
};

Thanks in advance for help

It is legal forward-declare something as a class and then define it as
a struct (http://www.gotw.ca/publications/mill04.htm).

Cheers! --M
 
P

Phlip

mlimber said:
It is legal forward-declare something as a class and then define it as
a struct (http://www.gotw.ca/publications/mill04.htm).

The book, /Exceptional C++/, upgrades that situation. The XImpl is now a
private struct inside X, and is also a struct in its implementation.

So the book no longer backs up your assertion, and my compiler(s) also do
not back it up.

To the original poster: Don't do that. Fix the types - to struct if they
contain data, or class if they contain methods.
 
N

nurxb01

Thanks "mlimber" and "Phlip" . I'll change the code to reflect the
proper type.

Can you give me an idea of what kind of problem may arise due to this
improper use of "class" in place of "struct" .

Thanks again.
 
B

Ben Pope

Thanks "mlimber" and "Phlip" . I'll change the code to reflect the
proper type.

Can you give me an idea of what kind of problem may arise due to this
improper use of "class" in place of "struct" .

Nothing in terms of the language, only in the understanding of people
reading your code without really paying attention.

The only difference between a struct and a class is the default access
of the members and base:

Given a base class:

struct base {};

Then:

struct s : base {
int i;
}

Is the same as

struct s : public base {
public:
int i;
}

Since struct has a defautl public access.

It's also the same as:

class : public base {
public:
int i;
}

class has default private access, but public access can be specified.


Similarly for:
class s : base {
int i;
}

is the same as:
struct s : private base {
private:
int i;
}


struct in C (not C++) can only be POD (plain old data), so there can be
no constructors, destructors, member funcions etc. This limitation is
removed in C++. C++ also has the keyword class.

I, and probably many others, only use struct for POD types to emphasise
their PODness. I also use them in example code where access rights are
not important, in order to keep the example as short as possible.

Ben Pope
 
T

Tomás

posted:
Hi
I have a very basic doubt.
The application i'm working on has some old code already in place.
There are some struct data type decleared in a .h file and are being
used for RPC communication.
Further in the application code at some places forward declearion
for these datatypes are given as class .
The code compiles without any errors and warning on solaris (g++)and
linux (g++)but give Compiler Warning C4099 on windows (MSDEV VC6.0) but
get compiled without any erros.
---------Description of warning C4099 starts ( from
msdn)-----------------------------
Compiler Warning (level 2) C4099
'identifier' : type name first seen using 'objecttype1' now seen using
'objecttype2'

The specified object was declared as either a structure or a class but
was defined as a class or a structure, respectively.
-----------------------------Description ends
------------------------------------

I never observe any problem in the execution of the program on ( Win2k
, solraris 2.7, RH Linux 7.1 ).

But can the above mentioned code be cause any problem or it's
perfactly ok to use that code.

//To make it more clear below is the two example header files
//from file a.h
//
struct CompositePt
{
double Pt;
bool mode;
};
typedef struct CompositePt CompositePt;

//from file b.h
//
class CompositePt;

class SomeApp_Code {

int test_function(CompositePt& point );
};

Thanks in advance for help


I wouldn't see any problem with that. Similarly you should be able to do:

signed main() {}

or:

signed int main() {}

or:

typedef signed monkey;

monkey main() {}


-Tomás
 
H

Herb Sutter

mlimber said:
It is legal forward-declare something as a class and then define it as
a struct (http://www.gotw.ca/publications/mill04.htm).

Yes, it is.

I used to do that occasionally, but today I generally don't, for the basic
reason that needly inconsistency isn't usually a good idea. Here are two
main reasons not to forward-declare as struct and then define as class (or
vice versa):

1. It can confuse people reading the code, who don't know the rule. It's
generally good to write nonobfuscated code that's easy for people to read
and maintain.

2. It can confuse (nonconforming) compilers. In the past, some popular
compilers name-mangled classes and structs differently, so that taking
advantage of the latitude didn't actually work on those compilers..

Herb
 
P

Phlip

Tomás said:
I wouldn't see any problem with that. Similarly you should be able to do:

typedef signed monkey;

monkey main() {}

That's not the same. Typedef creates an alias for a type. 'struct' and
'class' are different keywords. You would not use a 'struct' to
forward-declare an 'enum', for example.

Per Sutter's post here, the language designers decided to allow 'struct' and
'class' to forward-declare each other. That's more than an alias; it makes
the two identifiers become the same thing.
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top