473,698 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(C ompositePt& point );
};

Thanks in advance for help

Mar 13 '06 #1
8 2245
nu*****@gmail.c om 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(C ompositePt& 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.c om 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(C ompositePt& 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.c a> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2815
by: Marcin Kalicinski | last post by:
Hi All, In some headers meant to work with both C and C++ the following is often found: typedef struct tagMyStruct { /*some members*/ } MyStruct; Can I forward-declare MyStruct somehow? Or more generally, can I forward declare a typedef or an enum? Also, is
11
2765
by: Randy Yates | last post by:
I'm having a problem with forward references. For example, class DATE; class MYCLASS; class MYCLASS { public:
7
2866
by: Lynn | last post by:
I am rewriting some memory management code and I need to have a forward declaration of a data structure. I am not converting this data structure into a class (yet). How do I generate a forward reference of a data structure ? For a class, I just say: class SomeClass; "struct SomeStructure;" does not work to forward declare the data structure. This is the structure that I am trying to generate a vector of pointers to:
4
13248
by: yuliy | last post by:
Hello gurus, I stuck in following: how can I do forward declaration if the forward declared class is in some namespace? something like // header class std::string; // approach#1
3
4101
by: DhaneshNair | last post by:
Hi all, I hav a file which is actually linkage file (used as an reference interface between c and c++ files). And this file has got two structures in it .. When i include this file directly i dont have any issues. but according to some norms in the review I am not supposed to use this file direclty in the header file .. but its fine if i use that inside the corresponding file. To avoid compiler error, i forward declared the structure...
8
28460
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
2
1789
by: elmar_macek | last post by:
Hi all, i have recently stumbled about a problem: I have implemented a class GeneraBucket, which is superclass for Bucket and PBucket. I need a method in PBucket that returns a pointer to a Bucket: Bucket* PBucket::convertToBucket() {
6
1366
by: barcaroller | last post by:
Why does the following code cause a compiler error? class A; // forward reference class B { foo() { a = new A;
8
3725
boxfish
by: boxfish | last post by:
Hi everyone, I'm working on a 3d maze game, and I just messed up the classes so it won't compile, and I need help sorting it out. There's a class Maze, whose members are, among other things, a list of the walls, the user who is an object of class MazePerson, and a yellow square who is an object of class MazeMonster. MazePerson and MazeMonster are both inherited from class MazeThing, which takes care of stuff like collisions. MazePerson,...
0
9166
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.