accessing member function from another class

G

Guest

Hello,

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
{
run();
setConfigValues();
}

X::run()
{
Y y();
y.start();
}


Y::start()
{
X::setConfigValues();
}

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.
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.

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.

Thanks,
Enda
 
V

Victor Bazarov

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

class X

void run();
setConfigValues();

void setConfigValues();
;

void X::run()

Y y;
y.start();
}


Y::start()

void Y::start()
{
X::setConfigValues();

'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.

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.
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.

You need an instance of 'X'. Period.
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.

There is no such thing as "memory in class X". There are objects. They
have addresses. Their addresses can be passed around. Do it.
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.

Yes. See above.

V
 
G

Guest

Thanks, for pointing me in the right direction.

One more question.

Doing this

#include <X.h>

class Y
{
void start(X& x)
}

causes the following error

TestComponent.h(38) : error C2061: syntax error : identifier 'X'

Both classes are in separate files, do I need to declare friend
functions\classes. Or how can I get Class Y to see class X.

Thanks again
Enda
 
V

Victor Bazarov

Thanks, for pointing me in the right direction.

One more question.

Doing this

#include <X.h>

class Y
{
void start(X& x)
}

causes the following error

TestComponent.h(38) : error C2061: syntax error : identifier 'X'

Both classes are in separate files, do I need to declare friend
functions\classes. Or how can I get Class Y to see class X.

Read about "forward declaration". I trust you can figure it out without
me spelling it for you.

V
 
D

David Hilsee

Thanks, for pointing me in the right direction.

One more question.

Doing this

#include <X.h>

class Y
{
void start(X& x)
}

causes the following error

TestComponent.h(38) : error C2061: syntax error : identifier 'X'

Both classes are in separate files, do I need to declare friend
functions\classes. Or how can I get Class Y to see class X.

Since X knows about Y and Y knows about X, you may want to look at the FAQ
(http://www.parashift.com/c++-faq-lite/), Section 39 ("Miscellaneous
technical issues"), question 11 ("How can I create two classes that both
know about each other?").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top