Can a class be private to function call?

B

Brian Gianforcaro

All,

Is it possible to make a class only accessible to just a plain global
function?

Given the practices of this particular code base, the function is not
part of any class, it stands alone. The class this function uses as it's
main data structure will never (It is possible but highly unlikely) need
to be used by any other code in the code base.

I'm looking for a clean/elegant solution to make this class unaccessible
to other code.

As far as I can tell this isn't possible in c++, I'm hoping I am wrong.

Thanks
Any suggestions would be helpful,

- Brian Gianforcaro
 
A

Alf P. Steinbach

* Brian Gianforcaro:
Is it possible to make a class only accessible to just a plain global
function?

Given the practices of this particular code base, the function is not
part of any class, it stands alone. The class this function uses as it's
main data structure will never (It is possible but highly unlikely) need
to be used by any other code in the code base.

I'm looking for a clean/elegant solution to make this class unaccessible
to other code.

As far as I can tell this isn't possible in c++, I'm hoping I am wrong.

You can

* Use a local class.

* Use a class defined in an anonymous namespace in implementation file.

* Document the restriction (e.g., put it in namespace "detail", which
is a common convention for that).

Cheers & hth.,

- Alf
 
B

Balog Pal

"Brian Gianforcaro"
Is it possible to make a class only accessible to just a plain global
function?

void func()
{
class Hidden
{
}; // defined here
// used here
}
 
R

red floyd

* Brian Gianforcaro:







You can

   * Use a local class.

   * Use a class defined in an anonymous namespace in implementation file.

   * Document the restriction (e.g., put it in namespace "detail", which
     is a common convention for that).

You forgot:

* make all members private, and let the external function be its
only friend.
 
A

Alf P. Steinbach

* red floyd:
You forgot:

* make all members private, and let the external function be its
only friend.

Yeah, thanks.

Plus,

* make the function a static member of the class, where it's the only
public member.

Well OK it's almost the same as the friendship thing. :)


Cheers,

- Alf
 
J

James Kanze

* Document the restriction (e.g., put it in namespace
"detail", which is a common convention for that).

Not really relevant to the thread, but do you have any
references for the use of "detail" in this context. I've always
used "private"---or "Private", since for various historical
reasons, I use camel case. But I'm not aware of any convention
concerning this. (I think private suggests more of what is
wanted.)
 
A

Alf P. Steinbach

* James Kanze:
Not really relevant to the thread, but do you have any
references for the use of "detail" in this context.

See e.g. <url:
http://www.google.com/codesearch?q="namespace+detail"+lang:c++>.

46.300 hits.

:)

I've always
used "private"---or "Private", since for various historical
reasons, I use camel case.

180 hits.

I think the main reason for using initial uppercase there has to do with
"private" being a keyword.

But I'm not aware of any convention
concerning this. (I think private suggests more of what is
wanted.)

I agree with your reasoning, but the numbers seem to indicate that the Boost
convention, or whoever was there first, has established itself firmly, sort of
like MS-DOS in its time -- didn't matter if e.g. QNX was better.


Cheers,

- Alf

PS: I never used QNX, shame.
 

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
474,161
Messages
2,570,892
Members
47,428
Latest member
RosalieQui

Latest Threads

Top