Calling Private Method in C++

C++ : How do I call private methods through public ones?

You already have your class:

class ClassOne {
private:
void methodOne();

public:
void methodTwo();
};

Implement the functions of your class:

void ClassOne::methodOne() { // <-- private
// other code
}

void ClassOne::methodTwo() { // <-- public
// other code
methodOne(); // <-- private function called here
}

Calling private method in C++

#include the header file, but:

#define private public
#define class struct

Clearly you'll need to get around various inclusion guards etc and do this in an isolated compilation unit.

EDIT:
Still hackish, but less so:

#include <iostream>

#define private friend class Hack; private

class Foo
{
public:
Foo(int v) : test_(v) {}
private:
void bar();
int test_;
};
#undef private
void Foo::bar() { std::cout << "hello: " << test_ << std::endl; }

class Hack
{
public:
static void bar(Foo& f) {
f.bar();
}
};

int _tmain(int argc, _TCHAR* argv[])
{
Foo f(42);
Hack::bar(f);
system("pause");
return 0;
}

Make a private function in C

If you declare a function as static, it will only be visible by that name from inside that source file (or more accurately, from inside that translation unit):

static void f() {
....
}
static void g() {
f();
....
}
void public() {
f();
....
}

C++: Calling private function in a class from the public section of that class

You just call the private member functions from public ones:

class Foo
{
public:
void foo() { privateFoo(); }
private:
void privateFoo();

};

Calling private functions from another private function of the same class

Of course you can. You can always call a private function and access all class member data from any function within the class. That's what private does.

(Note that you can also access the private members of an instance of that class passed into a member function of that class. Although surprising at first, it's how you implement overloaded operators, copy constructors, &c.)

Call private method from the world in c++

Consider the following code, a modification of the code in the original question:

class A
{
public:
virtual void X(){
std::cout << "A::someMethod";
}
};
class B : public A
{
private:
virtual void Y(){
std::cout << "B::someMethod";
}
};
int main(){
A* a = new B;
a->X();
}

It's easy to understand that it's legit to call X(). B inherits it from A as a public member. Theoretically if X() would call Y() this would be legit as well of course, although this is not possible because X() is declared in A which doesn't know Y(). But actually this is the case if X = Y, i.e. if both methods have the same name.

You may think of it as "B inherits a public method from A, that calls a private method (of B) with the same name".

How to call private member function by using a pointer

Add a public method to your class that returns a pointer to the private function:

class A
{
private:
void error(void);
public:
void callError(void);

auto get_error_ptr() {
return &A::error;
}
};

int main(void)
{
void (A::*abc)(void) = &A::callError;
A test;

(test.*abc)();

void (A::*error_ptr)(void) = test.get_error_ptr();
(test.*error_ptr)();

return (0);
}

But I wouldn't suggest actually using this kind of code in a real application, it is extremely confusing and error-prone.

call Private function in c++

Is the private method also exported? I doubt it (why would they export a private method?). But if it is exported, you can use the dumpbin /exports command to see the decorated method name, then use GetProcAddress to get a function pointer to the method and call on an instance of the class. Something like:

HMODULE hModule = LoadLibrary(L"thedll.dll");
(C_MORPHO_Device::*pMethod)(PUL) = reinterpret_cast<(C_MORPHO_Device::*)(PUL)>(GetProcAddress(hModule, L"InitUsbDevicesNameEnum@_ABunchOfSymbolsHere"));

C_MORPHO_Device device;
(device.*pMethod)(...);


Related Topics



Leave a reply



Submit