473,785 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Partial classes

Can one define a partial class (first part containing constants like
enums) and then define the rest of the class elsewhere?

I ask this because I've had in the past needed to defined two classes
(call them A and B), each dependent upon the other. Class A requires
the enum type defined in Class B. An example follows:

<header file="A.h">
#if !defined A_H
# define A_H

// Stub for forward referencing
class A;

# include “B.h”

class A
{
// Interface functions
void f(B* b, B::b_e enums);
...
};
#endif
</header>

<header file="B.h">
#if !defined B_H
# define B_H

// Stub for forward referencing
class B;

# include “A.h”
class B
{
public:
enum b_e { enum1, enum2 };

// Interface functions
...
};
#endif
</header>

The only way I see around this are:
1. Declare a public base class for B that contains the enums. It is not
dependent on A so no problem occurs. It would be declared with the
class B stub;
2. Declare the enums outside of class scope, removing the dependency
lock. It would be declared with the class B stub;
3. Make a faux template class, making the class a stub. Haven't tried
this.

Problems for each are:
1. Base classes would have a explicit size of 1, adding a small amount
of overhead.
2. Enum is not scoped to a class.
3. A bit overkill if it does work.

Any thoughts?
Adrian

--
=============== =============== =============== ===========
Adrian Hawryluk BSc. Computer Science
--------------------------------------------------------
Specialising in: OOD Methodologies in UML
OOP Methodologies in C, C++ and more
RT Embedded Programming
__------------------------------------------------__
-----[blog: http://adrians-musings.blogspot.com/]-----
'------------------------------------------------------'
This content is licensed under the Creative Commons
Attribution-Noncommercial-Share Alike 3.0 License
http://creativecommons.org/licenses/by-nc-sa/3.0/
=============== =============== =============== ============
Mar 11 '07 #1
7 4353
Adrian Hawryluk wrote:
Can one define a partial class (first part containing constants like
enums) and then define the rest of the class elsewhere?
No.
>
I ask this because I've had in the past needed to defined two classes
(call them A and B), each dependent upon the other. Class A requires
the enum type defined in Class B. An example follows:

<header file="A.h">
#if !defined A_H
# define A_H

// Stub for forward referencing
class A;

# include “B.h”

class A
{
// Interface functions
void f(B* b, B::b_e enums);
...
};
#endif
</header>

<header file="B.h">
#if !defined B_H
# define B_H

// Stub for forward referencing
class B;

# include “A.h”
class B
{
public:
enum b_e { enum1, enum2 };

// Interface functions
...
};
#endif
</header>

The only way I see around this are:
1. Declare a public base class for B that contains the enums. It is
not dependent on A so no problem occurs. It would be declared
with the class B stub;
That's the best solution, IMO.
2. Declare the enums outside of class scope, removing the dependency
lock. It would be declared with the class B stub;
3. Make a faux template class, making the class a stub. Haven't tried
this.
No sure what you mean here.
Problems for each are:
1. Base classes would have a explicit size of 1, adding a small amount
of overhead.
Not to the class inheriting from it.

class Base {};
class Derived : public Base { int a; };
#include <iostream>
int main() {
std::cout << "sizeof(Bas e) = " << sizeof(Base) << std::endl;
std::cout << "sizeof(Bas e in Derived) = "
<< sizeof(Derived) - sizeof(int) << std::endl;
}
2. Enum is not scoped to a class.
3. A bit overkill if it does work.

Any thoughts?
Base class!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 11 '07 #2
Adrian Hawryluk wrote:
Can one define a partial class (first part containing constants like
enums) and then define the rest of the class elsewhere?
What's the real problem you need to solve; the problem that lead you to this
attempted solution?
Problems for each are:
1. Base classes would have a explicit size of 1, adding a small amount
of overhead.
Google "premature optimization". Always go for the cleanest design first,
regardless of optimization. It's easier to make beautiful code fast than
fast code beautiful.

And in this specific case, C++ can optimize that stub size away.
2. Enum is not scoped to a class.
So what? What's the worst thing that could happen if some plebe class gets
its dirty hands on that enum?
3. A bit overkill if it does work.
Faux template classes are where it's at. Declare the enum somewhere, and
template them into your target class.

In you unit tests, you can create different enums to test the template. (You
do _have_ unit tests, don't you?)

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Mar 11 '07 #3
BTW, your class B is not dependent on class
A at all. (A is dependent on B though) based on
this example code.
>
<header file="A.h">
#if !defined A_H
# define A_H
Remove these two lines. Not required.
B is not dependent on A.
// Stub for forward referencing
class A;

# include “B.h”

class A
{
// Interface functions
void f(B* b, B::b_e enums);
...
};
#endif
</header>

<header file="B.h">
#if !defined B_H
# define B_H
Remove these two lines also. B is already
included in A.h and is thus takes care of itself.
// Stub for forward referencing
class B;
B is not dependent on A so you can remove this too.
# include “A.h”
class B
{
public:
enum b_e { enum1, enum2 };

// Interface functions
...
};
#endif
</header>
Can you describe your problem or post actual code that
demonstrates your need for partial class declarations?
Mar 11 '07 #4
Victor Bazarov wrote:
Adrian Hawryluk wrote:
>Can one define a partial class (first part containing constants like
enums) and then define the rest of the class elsewhere?

No.
Yeah, I figured as much.
>The only way I see around this are:
1. Declare a public base class for B that contains the enums. It is
not dependent on A so no problem occurs. It would be declared
with the class B stub;

That's the best solution, IMO.
Hmmm.

Phlip wrote:
What's the real problem you need to solve; the problem that lead you
to this attempted solution?
The problem has occurred many times now. I'm not a newbie to C++, I've
been programming in it for 10 years now. I was just wondering if
anybody had another idea.
Google "premature optimization". Always go for the cleanest design
first, regardless of optimization. It's easier to make beautiful code
fast than fast code beautiful.

And in this specific case, C++ can optimize that stub size away.
Yeah, I know. Trouble is, there are compilers that f*** it up slowing
down certain apps because of it. I'm not optimising prematurely, as
I've said, I've been doing this for years.
>2. Enum is not scoped to a class.

So what? What's the worst thing that could happen if some plebe class
gets its dirty hands on that enum?
Wrong, that is probably closer to the best case, the worst case is you
get a name clash. And don't tell me about namespaces, it is OT from my
original question. I just want to keep the enum in place where it is used.
>3. A bit overkill if it does work.

Faux template classes are where it's at. Declare the enum somewhere,
and template them into your target class.
Oh yeah, this also exposes the code, not what I want.
In you unit tests, you can create different enums to test the
template. (You do _have_ unit tests, don't you?)
I didn't come here to be insulted, just ask a question. Lol.

Piyo wrote:
BTW, your class B is not dependent on class
A at all. (A is dependent on B though) based on
this example code.
Sorry, is this better?

<header file="A.h">
#if !defined A_H
# define A_H

// Stub for forward referencing
class A;

# include “B.h”

class A
{
// Interface functions
void f(B* b, B::b_e enums);
...
};
#endif
</header>

<header file="B.h">
#if !defined B_H
# define B_H

// Stub for forward referencing
class B;

# include “A.h”
class B
{
public:
enum b_e { enum1, enum2 };

void f(A* a); ///< here is the dependent
// Interface functions
...
};
#endif
</header>
Can you describe your problem or post actual code that
demonstrates your need for partial class declarations?
As for the actual example, it would be too long and have lots of extra
non-relevant stuff, but trust me it does occur and has occurred at other
times before that.

One case would be in a Mediator pattern when a mediator 'A' requires the
ids from a mediator 'B' to access B's objects indirectly through B.

If the base class is the only solution that can be thought up, then so
be it.
Adrian
--
=============== =============== =============== =============
Adrian Hawryluk BSc. Computer Science
----------------------------------------------------------
Specialising in: OOD Methodologies in UML
OOP Methodologies in C, C++ and more
RT Embedded Programming
__--------------------------------------------------__
----- [blog: http://adrians-musings.blogspot.com/] -----
'--------------------------------------------------------'
My newsgroup writings are licensed under the Creative
Commons Attribution-Noncommercial-Share Alike 3.0 License
http://creativecommons.org/licenses/by-nc-sa/3.0/
=============== =============== =============== =============
Mar 12 '07 #5
Adrian Hawryluk wrote:
Sorry, is this better?
BTW still based on the example, only A is dependent
on B fully. B does not need a full include in the
header (in the .cpp yes but not the header). A forward
declaration of class A in B is sufficient in the header.
But I am sure you know this :)
>
<header file="A.h">
#if !defined A_H
# define A_H

// Stub for forward referencing
class A;

# include “B.h”

class A
{
// Interface functions
void f(B* b, B::b_e enums);
...
};
#endif
</header>

<header file="B.h">
#if !defined B_H
# define B_H

// Stub for forward referencing
class B;

# include “A.h”
class B
{
public:
enum b_e { enum1, enum2 };

void f(A* a); ///< here is the dependent
// Interface functions
...
};
#endif
</header>
As for the actual example, it would be too long and have lots of extra
non-relevant stuff, but trust me it does occur and has occurred at other
times before that.
I do understand that it does occur and based on your suggestions, I
would do:

1) Hopefully your compiler is smart to do empty base class optimization
to eliminate your overhead.
1. Base classes would have a explicit size of 1, adding a small amount
of overhead.
Check this for reference about the optimization:
http://www.cantrip.org/emptyopt.html

2) doing the base class thing also kind of implies to me that you may
opt to namespace your enumerated independent of both class A and class
B and you can even reintroduce the enumerated type back into class B
if that is even necessary.

HTH
Mar 12 '07 #6
Piyo wrote:
Adrian Hawryluk wrote:
>Sorry, is this better?
BTW still based on the example, only A is dependent
on B fully. B does not need a full include in the
header (in the .cpp yes but not the header). A forward
declaration of class A in B is sufficient in the header.
But I am sure you know this :)
>[snip]
Thanks, I do. I do it this way for (primarily) auto dependency
generation. If I were to change something in B that may have to rely on
some information in A later, I don't want to have to fiddle around with
the header file, or do a clean build of the entire system when it is not
necessary. It is a template setup that I prefer. But thank you again
for pointing it out. :)
>As for the actual example, it would be too long and have lots of extra
non-relevant stuff, but trust me it does occur and has occurred at
other times before that.

I do understand that it does occur and based on your suggestions, I
would do:

1) Hopefully your compiler is smart to do empty base class optimization
to eliminate your overhead.
>1. Base classes would have a explicit size of 1, adding a small amount
of overhead.

Check this for reference about the optimization:
http://www.cantrip.org/emptyopt.html
Thanks.
2) doing the base class thing also kind of implies to me that you may
opt to namespace your enumerated independent of both class A and class
B and you can even reintroduce the enumerated type back into class B
if that is even necessary.
Putting the enum in the class is more for ownership reasons to state
loud and clear that the enum's primary purpose is for that class without
adding a new namespace to the system, while avoiding name clashing.
Adrian
--
=============== =============== =============== =============
Adrian Hawryluk BSc. Computer Science
----------------------------------------------------------
Specialising in: OOD Methodologies in UML
OOP Methodologies in C, C++ and more
RT Embedded Programming
__--------------------------------------------------__
----- [blog: http://adrians-musings.blogspot.com/] -----
'--------------------------------------------------------'
My newsgroup writings are licensed under the Creative
Commons Attribution-Noncommercial-Share Alike 3.0 License
http://creativecommons.org/licenses/by-nc-sa/3.0/
=============== =============== =============== =============
Mar 12 '07 #7
On Mar 12, 3:51 am, Adrian Hawryluk <adrian.hawrylu k-at-
gmail....@nospa m.comwrote:
Can one define a partial class (first part containing constants like
enums) and then define the rest of the class elsewhere?

#if !defined A_H
# define A_H

// Stub for forward referencing
class A;

# include "B.h"
This is not a recommended style; A.h should not both forward-declare
and define A. The forward declaration should be inside B.h . I assume
B.h requires A to be declared; what are you going to do if some other
unit includes B.h and not A.h ? Repeat the forward declaration in
every other unit?
class A
{
// Interface functions
void f(B* b, B::b_e enums);
...
};
#endif

The only way I see around this are:
2. Declare the enums outside of class scope, removing the dependency
lock. It would be declared with the class B stub;
Problems for each are:
2. Enum is not scoped to a class.
This isn't really much of a problem, to me. If you want to force
others to use B:: before the enum values and typename then you can
write B.h like this:

namespace B {
enum b_e { ....... };
};

class B {
..............
};

Then you have to use B::value for the enum values, even within B.cpp;
if you don't want this then you can include "using namespace B"
inside B.cpp .

Mar 13 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
2677
by: nospam | last post by:
I got three (3) files (1) Untitled.aspx (2) Untitled.aspx.1.cs (3) Untitled.aspx.2.cs These three files must be used together to make file #1, Untitled.aspx, page work via J.I.T. when the User first hits Internet Explorer 6.0 on your browser.
9
2534
by: Gomaw Beoyr | last post by:
Two question about the "partial classes" (in the next wersion of ..NET). Question 1 ========== Will partial classes (in the next version of C#) have to be declared "partial" in ALL places. I.e. do we have to need to write:
16
2652
by: pawel.pabich | last post by:
Hajo, I would like to have 2 my own partial classes. For example: Default.aspx.cs Default2.aspx.cs and they both will relate to Default.aspx page.
10
2446
by: ptass | last post by:
Hi In asp.net 2.0 an aspx files .cs file is a partial class and all works fine, however, I thought I’d be able to create another class file, call it a partial class and have that compile and load as a 3rd partial class. This would be handy so i can generate standard code into one of the partial classes, while having my custom code untouched
1
2637
by: Bishoy George | last post by:
In a web application using asp.net 2.0 All my classes are partial classes. - I noticed that partial class cannot be inherited... is that true? - I tried to remove the partial keyword , and I receieved this error Error 1 Missing partial modifier on declaration of type 'NagyResearch.Q_A.Q.QuestionnairePage'; another partial declaration of this type exists D:\Web Sites\Local
9
5789
by: Fat Elvis | last post by:
I'd like to extend some of my Asp.net pages by using Partial Classes. Example ASP.Net Page: public partial class Admin_Customer : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Data_List(); } }
2
1431
by: Sebastian | last post by:
Hi Everyone, we're currently developing an web-app for insurance company. We've got a long form-flows and view-classes (MVC) of about 2000 lines of code. Is there any possibility to split code-behind classes into multiple files using partial classes? It would be helpfull to balance work on multiple developers working on same subject. I've already got it done in DLL-Projects (SQL-Helper, Builders, .... ) but how to do it in web-projects?
5
1374
by: Sagar | last post by:
I am working on a migration project for a huge asp.net application now running in framwework 1.1 to dotnet 2.0 As a first step, I opened the .sln files of the project in VS.Net 2005. The Conversion wizard created partial classes off the existing classes. Is partial classes mandatory in VS.Net 2005 or can I still work without having to live with partial classes ?
2
2128
by: Peted | last post by:
Hi, im moving a project from vs2005 to vs 2008. is doing so i have come across a compiler error regarding partial classes that i dont understand if anyone can explain it to me please the orig defintion that compiles and runs fine in vs2005 is bellow......
10
1538
by: JDeats | last post by:
So I have a class that spans over two partial classes in code, here's an example (do not read much into this, the code is of no practical use, this is just a simple example of where my confusion occurs). // Inside SharedClassExample1.cs public partial class SharedClassExample { public List<stringBooksOnShelf { get; set; } public List<stringBooksOnDesk { get; set; }
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9959
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
8988
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
7510
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
5397
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
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
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
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.