🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1450

Re: Proposal for default scope

#1450
From: David Cok
Date: Mon, 11 Jan 1993 15:36
72 lines
2701 bytes
In article <1993Jan8.044714.2027@qualcomm.com> greg@qualcom.qualcomm.com (Greg Noel) writes:
>
>That is,
>	class Class {
>	public:  int func1();
>	};
>	Class:: {
>		int helper() { /* ... */ }
>		int func1() { /* code including calls to helper() */ }
>	}
>Since helper() is not declared in the class, it is not exported from the
>scope, while func1() is declared in the class, so it is exported in the
>normal fashion.
>

I agree fully with your desire to provide helper functions with access to
a class which do not appear in the class declaration and thus do not
cause recompilation when they are introduced or removed.  This is how I
do it, sticking to existing C++.  It's not as good as having language 
facilities to add helper functions into a class without affecting the 
header file, but for the moment it is adequate.  The drawbacks are 
introduction of another class name (which is publically visible), the 
need for qualification of the helper functions, and being careful about 
derived classes of Class when used in Class_helper.

Class.h:

class Class {
	friend class Class_helper;
	private: void func_private();
	public: int func1();
};


Class.C:

#include "Class.h"

class Class_helper {
	friend class Class;
private:
	static int helper() { ... }
	static int helper2(Class* c) { c->func_private(); ... }
};

typedef Class_helper Ch; // abbreviation 
static Class_helper CH; // just for convenience -- see usage examples below

void Class::func_private() { ... }

int Class::func1() {
	Class_helper::helper(); // take your pick of these three alternatives
	Ch::helper();
	CH.helper();

	Ch::helper2(this);
}


It would be nice to be able to make Class_helper a nested class within Class
while still declaring it in Class.C; but if that were made possible, I imagine 
that the problem which gave rise to this programming idiom could be fixed
directly.

One can also derive Class_helper from Class to get at Class's member functions
directly.  Then helper2 does not need to be static and does not need an
argument, but there would need to be a conversion from Class* to Class_helper, 
so there is not a lot of gain.

David R. Cok
Eastman Kodak Company
cok@Kodak.COM
Technical Details
Message-ID: <1993Jan11.153602.22907@kodak.kodak.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!think.com!rpi!newsserver.pixel.kodak.com!kodak!acadia!cok
References: <mkohtala.726108774@vipunen.hut.fi> <1993Jan8.044714.2027@qualcomm.com>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++
Path: arc1-ca.pugleaf.net!sparky!uunet!think.com!rpi!newsserver.pixel.kodak.com!kodak!acadia!cok
From: cok@acadia.Kodak.COM (David Cok)
Subject: Re: Proposal for default scope
Message-ID: <1993Jan11.153602.22907@kodak.kodak.com>
Sender: news@kodak.kodak.com
Organization: Eastman Kodak Co., Rochester, NY
References: <mkohtala.726108774@vipunen.hut.fi> <1993Jan8.044714.2027@qualcomm.com>
Date: Mon, 11 Jan 93 15:36:02 GMT
Lines: 72