There is a function
double funct(double);
It contains a call to some other functions. But they should be hidden by everything except this “main” function. How?
There is a function
double funct(double);
It contains a call to some other functions. But they should be hidden by everything except this “main” function. How?
In my opinion, a standard solution is appropriate here. It consists in the fact that two files are created:
my.h
#include <stdio.h> .... // столько инклюдов, сколько нужно // сделать guardian для единичного включения include-файла!!! // стандартно - через define-ifdef или pragma double funct(double); // прототип нашей ф-ции // не уверен, но возможно понадобится запись extern // для включения my.h в проект потребителя
my.cpp
#include <my.h> .... // код всех вспомогательных ф-ций double funct (double) {...} // код нашей ф-ции
If paranoia tortured - all auxiliary functions can be made static , i.e. they will only be available from my.cpp . Another option is not to distribute the my.cpp file to consumers, but provide them with an object module (obj file) or library (lib file).
An even more tricky method is to compile a dynamic library with the funct function so that it is in the export table ... Then the library guts will not be accessible from the outside (at least without tricks). This is probably good if you need to implement the concept of a black box. But is it necessary to dodge like that?
An interesting technique for restricting access from other modules is to use an unnamed namespace. Like or so
In general, it would be nice if the author of the question still told why he needed such a concealment ...
It can be so pretty cheap:
class MyPublic{ void funct( ... ){ return static_cast< MyPrivate* >( this )->priv_funct( ... ); } };
In some godforsaken namespace
(which MyPublic
should not forget), or directly in the funct'е
, if the code is not too cumbersome:
class MyPrivate : public MyPublic{ void something_hidden( ... ){ ... } void priv_funct( ... ){ return something_hidden( ... ); } };
Only MyPrivate
should not add any data to the ancestor, otherwise it will have to be taken into account. You can play with the inheritance access modifier, but if the goal was to get rid of helper methods in tools, like IntelliSense
, then that would be enough.
derived
classes. - Costantino Rupertdynamic_cast
is a search among physically-generated class vertices that are associated with vmt
. Those. This is a search in a real object, and here is a "phantom". It will only be found if the MyPrivate
object MyPrivate
actually created. Yes, and painfully, this is an expensive method - it is clearly no better. - megaCan be done as an inline function (based on the discussion )
void func () { struct hider { static void hidden_function() { //something } }; void (*hf) () = &hider::hidden_function; hf(); }
Source: https://ru.stackoverflow.com/questions/163998/
StaticHelperClass
, which will contain only static functions, declare itstatic double funct(double);
as public, and auxiliary static functions make private. - Costantino Rupert