Access private static method from outside the class C++

rathorsunpreet

How exactly can you access a private static method from outside the class. Say, I have a class

    Class ABC {
    private:
        static void print(string str) {
            cout << "It works!!!" << endl;
        }
    };

Now, I just to call print() function say from another function like:

    void doSomething() {
         string str = "1776a0";
         // Call to print() here
    }

I have searched the internet and stackoverflow for such a problem but I couldn't find much. So, please point me in the right direction as to if this is possible or not and if so how to do it.

I am currently using GCC.

Thank you all in advance.

M.M

You can't. That is exactly what private means. If it is intended to be callable from outside the class, make it public.

Instead of making it public you could call it from another function that is publicly accessible. This other function could either be a public member of ABC, or a friend function.

Both cases require changing the class definition of ABC.

If the other function just does nothing besides call print() then you have achieved the same effect as making print() public. But presumably print is private for a reason, e.g. it relies on some preconditions. You could make the other function allow for that. For example:

void abc_printer(string printer_name, string str_to_print)
{
    open_printer(printer_name);          
    ABC::print(str);                         
    close_printer();
}

and inside the class definition of ABC:

friend void abc_printer(string, string);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to access private class's property from public static method in PHP

From Dev

Access Class Method From Outside - React Native

From Dev

Access an object from another class in private method

From Dev

C++ - How to access private members of a class, from a static function of the same class?

From Dev

Why is it possible to access private variables from outside class by reference?

From Dev

How to access private static class member inherited from a template class?

From Dev

C++ public method inherited from base class can not access private member variable in derived class

From Dev

How to Access private parameterized method of private inner class with in a static class in JAVA

From Dev

Access property from outside a class in objective c

From Dev

Calling a non-static method from outside the class

From Dev

php class private property access outside class

From Dev

How to call a static method from a private base class?

From Dev

Why aren't we allowed to access a private static member outside class without public member function?

From Dev

C++ - Change private member from outside the class

From Dev

Ruby class with static method calling a private method?

From Dev

c++ access private static member from implementation file

From Dev

How to access from 'private method' to 'public variable', in Javascript class

From Dev

Access public static variable outside the class

From Dev

Can you define a static method outside of a class?

From Dev

PHP: access a parent's static variable from an extended class' method

From Dev

PHP reference Class from variable with static method access

From Java

Class can't access its own private static constexpr method - Clang bug?

From Dev

TypeScript: access static method of a class

From Dev

Stub only one private static method in a class

From Dev

How to access an object in c sharp from outside my method?

From Dev

Private var is accessible from outside the class

From Dev

Calling private function in lambda from outside the class

From Dev

C# How to access static class List<> from another class

From Dev

Modify class private outside class without using public method

Related Related

  1. 1

    How to access private class's property from public static method in PHP

  2. 2

    Access Class Method From Outside - React Native

  3. 3

    Access an object from another class in private method

  4. 4

    C++ - How to access private members of a class, from a static function of the same class?

  5. 5

    Why is it possible to access private variables from outside class by reference?

  6. 6

    How to access private static class member inherited from a template class?

  7. 7

    C++ public method inherited from base class can not access private member variable in derived class

  8. 8

    How to Access private parameterized method of private inner class with in a static class in JAVA

  9. 9

    Access property from outside a class in objective c

  10. 10

    Calling a non-static method from outside the class

  11. 11

    php class private property access outside class

  12. 12

    How to call a static method from a private base class?

  13. 13

    Why aren't we allowed to access a private static member outside class without public member function?

  14. 14

    C++ - Change private member from outside the class

  15. 15

    Ruby class with static method calling a private method?

  16. 16

    c++ access private static member from implementation file

  17. 17

    How to access from 'private method' to 'public variable', in Javascript class

  18. 18

    Access public static variable outside the class

  19. 19

    Can you define a static method outside of a class?

  20. 20

    PHP: access a parent's static variable from an extended class' method

  21. 21

    PHP reference Class from variable with static method access

  22. 22

    Class can't access its own private static constexpr method - Clang bug?

  23. 23

    TypeScript: access static method of a class

  24. 24

    Stub only one private static method in a class

  25. 25

    How to access an object in c sharp from outside my method?

  26. 26

    Private var is accessible from outside the class

  27. 27

    Calling private function in lambda from outside the class

  28. 28

    C# How to access static class List<> from another class

  29. 29

    Modify class private outside class without using public method

HotTag

Archive