keepyourstupidspam@yahoo.co.uk wrote:[color=blue]
> I have a class X with a member function setConfigValues(). I want to
> access this member function in another class Y. Y is not inherited from
> X. A member function of class X called run() this instanciates the
> class Y.
>
> Class X[/color]
class X
[color=blue]
> {
> run();[/color]
void run();
[color=blue]
> setConfigValues();[/color]
void setConfigValues();
[color=blue]
> }[/color]
;
[color=blue]
>
> X::run()[/color]
void X::run()
[color=blue]
> {
> Y y();[/color]
Y y;
[color=blue]
> y.start();
> }
>
>
> Y::start()[/color]
void Y::start()
[color=blue]
> {
> X::setConfigValues();[/color]
'setConfigValues' is a non-static member. It needs an _instance_ of 'X'
to be called. Since it seems that 'Y' is not related to 'X', you can't
call 'setConfigValues' like that.
[color=blue]
> }[/color]
If you mean to call 'setConfigValues' for the same object 'X::run' was
called for, then you need to do
void X::run()
{
Y y;
y.start(*this);
}
void Y::start(X& x)
{
x.setConfigValues();
}
IOW, you need to convey to the 'Y' what 'X' to call 'setConfigValues' for.
[color=blue]
> I can't make setConfigValues() a static function because it accesses
> member functions and variables that are not static.
>
> What is the best way of accessing setConfigValues() from other classes.[/color]
You need an instance of 'X'. Period.
[color=blue]
> There are other objects instanciated in class X that setConfigValues()
> uses so don't think I can use friend functions. I need to access
> setConfigValues() as it is in memory in class X.[/color]
There is no such thing as "memory in class X". There are objects. They
have addresses. Their addresses can be passed around. Do it.
[color=blue]
> Hopes this makes sence and wonder what is the best way of doing it.
>
> If the best way is by passing a reference or pointer how do I do this.[/color]
Yes. See above.
V