472,125 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,125 software developers and data experts.

can forward declearation of struct as class cause any problem

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

Mar 13 '06 #1
8 2122
nu*****@gmail.com wrote:
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

Mar 13 '06 #2
mlimber wrote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 13 '06 #3
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.

Mar 14 '06 #4
nu*****@gmail.com wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Mar 14 '06 #5
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

Mar 14 '06 #6
"mlimber" <ml*****@gmail.com> wrote:
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

---
Herb Sutter (www.gotw.ca) (www.pluralsight.com/blogs/hsutter)

Convener, ISO WG21 (C++ standards committee) (www.gotw.ca/iso)
Architect, Developer Division, Microsoft (www.gotw.ca/microsoft)
Mar 14 '06 #7
Tomás wrote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 14 '06 #8
In message <4t********************************@4ax.com>, Herb Sutter
<hs*****@gotw.ca> writes
"mlimber" <ml*****@gmail.com> wrote:
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..

At least one popular compiler still does. I was bitten by exactly that
problem porting some code to Visual Studio .NET this very day.

--
Richard Herring
Mar 15 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Marcin Kalicinski | last post: by
11 posts views Thread by Randy Yates | last post: by
7 posts views Thread by Lynn | last post: by
4 posts views Thread by yuliy | last post: by
8 posts views Thread by Mohammad Omer Nasir | last post: by
6 posts views Thread by barcaroller | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.