Im new to C++ and i wonder if anyone cam help
- Code:
class whatever
{
private ;
int m_int;
public:
void func1();
void func2();
};
My problem is that i want func2 to be part of the class but i dont want it to be seen so my class should look like
- Code:
class whatever
{
private ;
int m_int;
public:
void func1();
};
if i put func2 in the c++ file would this be correct or can anyone help me with this, basically i want to hide the function so the user cannot see but the class can call it internally.
Put func2 in a private section if you only want to access it from this object, or in a protected section if you intend to inherit from it and want inherited classes to be able to access it.
- Code:
class whatever
{
private :
int m_int;
void func2();
public:
void func1();
};
thanks for the answer, but i guessed i could do that.
but what i would like to be able to do is have the function(oops member) not seen at all, i guess i cannot do this..?
I thought you might want that, but then thought that would be too silly to ask for

Remember that your compiler will be reading the same source as you.
You could (potentially) compile a function externally and link it in... but I'm not entirely sure that would be what you want either.
What exactly do you want to accomplish?
I have gone for an easy option ive put a compiler directive in my implementation and got rid of func2 so the code is doing what i want but i would have liked seperate functions.
I know what i asked for is silly, but as i am not used to c++ i thought there may be a way to hide the function without making it private anyway now i know
thanks
Making the funciton private ensures that only the class itself and its friends can call it. This effectively does what you wish. But the function is still declared in the class in the header file. Other code can still "see" it. If it changes in any way, other code has to be recompiled, since even though they do not call it, it is part of the class they are using.
If you remove the function from the class, and instead make it a free function in the .cpp file, this will help, but let me explain.
First of all, doing so will mean that even your header file cannot call the function. So if you have inline functions, they cannot call that function.
Secondly, just putting the function in the .cpp file does not hide it. With a proper declaration, another file can still call it and link to it.
In the old days, therefore, people used to declare such functions static to make sure the linker did not export them.
However, in modern times, we do that by putting the function in an anonymous namespace. That is the solution I would recommend if you want a function "in the class" to only be called from within that class's .cpp file.
If you really want the an ultimate solution to this problem, particularly for data members you wish to "hide", do a web search for something called the "pimpl idiom". But that's probably way more than you need for what you want to do.