By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,812 Members | 1,650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,812 IT Pros & Developers. It's quick & easy.

accessing member function from another class

P: n/a
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

Aug 31 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ke****************@yahoo.co.uk wrote:
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
{
run();
void run();
setConfigValues();
void setConfigValues();
} ;

X::run()
void X::run()
{
Y y();
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
Aug 31 '05 #2

P: n/a
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

Aug 31 '05 #3

P: n/a
ke****************@yahoo.co.uk wrote:
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
Aug 31 '05 #4

P: n/a
<ke****************@yahoo.co.uk> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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?").

--
David Hilsee
Aug 31 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.