How to call on a private method in the main program?

Apr 27, 2012 at 2:38am
Hello!
I have a private method that is as follows:

void BEAM::calc_area(double b, double h)
{
A=b*h;
}

I want to call upon the method and pass the variables b and h to it from my main program. How would go about doing this?

Last edited on Apr 27, 2012 at 2:39am
Apr 27, 2012 at 7:46am
You could make it public.
Apr 27, 2012 at 9:12am
Cirabou,

The thing which you need is called pointer to class method.

for example this is one way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class MyClass
{
public:
	int calc_area(double b, double h)
	{
		cout << "Class method";
		return b * h;
	}
};

typedef int (MyClass::*METHOD) (double, double);

int main()
{
	MyClass* object = new MyClass;
	METHOD Action = &MyClass::calc_area;
	(object->*Action) (4.4, 3.3);
	delete object;
	cin.ignore();
	return 0;
} 

Apr 27, 2012 at 9:45am
what is the point of accessing a private function from outside of a class??? If you want to do it anyways then you can use friend functions.
Apr 27, 2012 at 9:47am
I never try this so I've just make a hipothetic test program :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

namespace Test
{
	class MyClass
	{
		friend void Access(MyClass* object, double x, double y)
		{
			typedef int (MyClass::*METHOD) (double, double);
			METHOD Action = &MyClass::calc_area;
			(object->*Action) (x, y);
		}

	private:
		int calc_area(double b, double h)
		{
			std::cout << "Class method";
			return b * h;
		}
	};
}

int main()
{
        using std::cin;
	using Test::MyClass;

	MyClass* object = new MyClass;
	Access(object, 4.4, 3.3);
	delete object;
	cin.ignore();
	return 0;
} 


Last edited on Apr 27, 2012 at 9:56am
Apr 27, 2012 at 10:14am
And this too..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
class MyClass;
typedef int (MyClass::*METHOD) (double, double);
class MyClass
{
private:
	int calc_area(double b, double h)
	{
		cout << "Class method";
		return b * h;
	}
public:
    static METHOD get_calc_area()
    {
        return &MyClass::calc_area;
    }
};



int main()
{
	MyClass* object = new MyClass;
	METHOD Action = MyClass::get_calc_area();
	(object->*Action) (4.4, 3.3);
	delete object;
	cin.ignore();
	return 0;
}


EDIT:
But then again, there's no point in doing all these things to access a PRIVATE member function of a class (except, perhaps in extreme conditions). Either use friends or make the PRIVATE member function public
Last edited on Apr 27, 2012 at 10:16am
Apr 27, 2012 at 10:22am
Yeah, the power of C++ !!!
Apr 27, 2012 at 10:37am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public:
	int calc_area(double b, double h) 
//a method that doesn't modify or use the state of the object in any way
//¿why is it a method then?
	{
		cout << "Class method";
		return b * h;
	}

//...
	MyClass* object = new MyClass; 
	METHOD Action = &MyClass::calc_area;
	(object->*Action) (4.4, 3.3); //extra indirection
	delete object; //new and delete in the same scope
	cin.ignore(); //unnecessary  
Please cut your left hand.

Edit:
1
2
MyClass object;
object.calc_area(4.4, 3.3);
However that doesn't respond the OP request.
A private method/member can't be accessed outside (that's why it is private). It is used as a helper, called by other methods (maybe the constructor in OP case).

So please don't post garbage.
Last edited on Apr 27, 2012 at 12:06pm
Apr 27, 2012 at 10:56am
@timer ic
what is that supposed to mean?
Apr 27, 2012 at 11:48am
ne555,

What?
Apr 27, 2012 at 11:55am
The question isn't how to access a private method, but rather why on earth you'd ever do that.
Apr 27, 2012 at 2:11pm
Thanks for the help, everyone! I figured it out. I didn't want to do it. Frankly, I want to be sleeping, but my professor seems to think it is necessary to my C++ career.
Oh well. Thanks again!
Topic archived. No new replies allowed.