Access Private member function of a class [duplicate] - c++

This question already has answers here:
How can I test private members and methods of classes?
(8 answers)
Closed 8 years ago.
I have a class A having a number of Private/Public member functions/variables. I have added a new private member function to it. As part of a testing the newly added method, I need to invoke the private member function from my test class. In the case of member variables I have seen an example like below:
#include <iostream>
using namespace std;
class test
{
private:
int myInt;
public:
int getInt () {return myInt;}
};
int main ()
{
test t;
int* p = (int*) & t;
*p = 20;
cout << t.getInt ();
}
Output:20
Is there any reliable way I can access the private member function such that there is no code modification in the class A?

You can change class access modifiers with macro #define private public for testing, but it isn't good idea. I think you need to review test approach

Related

How to call a class function in c++ without object declaration? [duplicate]

This question already has answers here:
Invoke a c++ class method without a class instance?
(5 answers)
Closed 1 year ago.
I have created a function within a class. How do I call that function in int main() without declaring any object for the class?
You can do it by using a static member function as shown below:
#include <iostream>
class NAME
{
public:
//define a static member function
static void print_st()
{
std::cout<<"static print_st callled"<<std::endl;
}
};
int main()
{
//call static member function without using an object
NAME::print_st();
return 0;
}
What you can do is to write your function inside the class as static. This lets you call the function without declaring an Object

reference to non static member must be called [duplicate]

This question already has answers here:
problem sorting using member function as comparator
(9 answers)
Closed 2 years ago.
Is there any way to declare the comp function here under the scope of the class declared like this.
class Solution {
public:
bool comp(vector<int> x, vector<int> y){
return x[0] < y[0];
}
vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(),intervals.end(),comp);
return {{}};
}
};
This code snippet gives error :
reference to non static member must be called
The compiler would say that a non-static member reference must be relative to a specific object. This hint states that you must need to create an object before accessing the class members if the members ain't declared as static.
There are two options here:
Either Declare the class member function as static and then directly access.
OTOH, create an object (instance) of the class and then access the class member function.
The first's solution would look something like:
class Solution {
public:
// Declared as 'static', so you don't need to create any instances
static bool comp(vector<int> x, vector<int> y) {
// ...
}
};
// ...
Solution::comp(...);
And the second solution would be (perhaps you didn't want this one):
class Solution {
public:
bool comp(...) {
// ...
}
};
// ...
Solution s;
s.comp(...);

Assign value to private static variable in a class [duplicate]

This question already has answers here:
Undefined reference to static class member
(9 answers)
How to initialize private static members in C++?
(18 answers)
Closed 6 years ago.
I have a file A.hpp as such:
class A
{
private:
static std::string s;
public:
void modify_string();
};
I am implementing this in a file A.cpp as such:
#include "A.hpp"
void A::modify_string()
{
s = "something"; // Error here.
}
My main class:
int main()
{
A a;
a.modify_string();
}
I understand static variables are shared by all the class instances. I also went through this SO post where it says how to access the static member. Public static member of class . Could you please let me know where my concept is missing at?
Edit:
I am getting this error:
error: undefined reference to A::s
When you define:
void modify_string() {
s = "something"; // Error here.
}
You are creating a new function, not defining the member function modify_string of the class A. You need to do:
void A::modify_string() {
To inform the compiler that you are defining the member function modify_string for class A.
You also need a ; after your class definition.
Finally, the variable s is static so it needs to be defined seperatly somewhere so the linker can find a reference to it. So add:
std::string A::s = "default";
This was clearly described in the link you provided for your question.
Here is a working example: http://ideone.com/iQ6Kux
You need to reserve storage for s in exactly one compilation unit.
Do that by writing
std::string A::s;
In exactly one source file.
Your definition void modify_string() {...} in A.cpp is not defining the member function of the class, it's defining a separate global function with the same name. You probably meant
void A::modify_string()
{
s = "something";
}

Map of pointers to member functions in another class [duplicate]

This question already has answers here:
Using generic std::function objects with member functions in one class
(6 answers)
Closed 7 years ago.
I know, that this question was already asked here, but I believe that my particular example is unique:
#include <functional>
#include <map>
#include <vector>
class Bar{
public:
static unsigned myFunc(const std::vector<std::string> &input){return 1;};
};
class Foo{
friend class Bar;
public:
using CommandFunction = std::function<unsigned(const std::vector<std::string> &)>;
std::map<std::string, CommandFunction> Commands;
};
int main(){
Foo oFoo;
Bar oBar;
oFoo.Commands["myFunc"] = oBar.myFunc;
return 0;
}
I want to make the myFunc function non-static, so it will be able to access private members of the Bar class. But I have no clue how to implement this idea. Simply removing the static keyword obviously will raise an error during compilation (invalid use of non-static function). Is there any 'clean' way of solving this problem? And by 'clean' I mean not using global variables and objects.
Update
I think I need to clarify the purpose of the design I described above.
I'm using a wrapper to the GNU readline library. It is represented by the Foo class. Basically it holds a set of function pointers inside the Commands map and executes them based on the user input.
The Bar class is a set of functions which share common resources (private members of the Bar class).
Lambda could be a solution for you:
class Bar{
public:
std::function<unsigned(const std::vector<std::string>&)> myFunc{ [](const std::vector<std::string>& x){return 1;}};
};

How to loop over the public variables of a class in c++? [duplicate]

This question already has answers here:
Is there a way of applying a function to each member of a struct in c++?
(13 answers)
Can I loop through (public) attributes of a C++ class?
(2 answers)
Closed 7 years ago.
How to loop (foreach public variable of class in c++), I have temp_class like the following
class temp_class
{
public:
std::string name;
int age;
class2 object_class;
private:
//some code here
}
Now I need to get a list of all public variable in the class (some thing similar if we want to get a list of all the properties of a c# class, but for c++ class)
temp_class object_class;
//here these is a code to set the values for each public variable in the object
foreach(public_variable in object_class)
{
string var_name=//get the public variable name
string var_value=//get the public variable value
}
You can't. this is C++ , unmanaged code without true reflection.