Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 31st, 2005, 05:15 PM
keepyourstupidspam@yahoo.co.uk
Guest
 
Posts: n/a
Default accessing member function from another class

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

  #2  
Old August 31st, 2005, 06:05 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: accessing member function from another class

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
  #3  
Old August 31st, 2005, 08:25 PM
keepyourstupidspam@yahoo.co.uk
Guest
 
Posts: n/a
Default Re: accessing member function from another class

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

  #4  
Old August 31st, 2005, 08:55 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: accessing member function from another class

keepyourstupidspam@yahoo.co.uk wrote:[color=blue]
> 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.[/color]

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

V
  #5  
Old September 1st, 2005, 12:15 AM
David Hilsee
Guest
 
Posts: n/a
Default Re: accessing member function from another class

<keepyourstupidspam@yahoo.co.uk> wrote in message
news:1125515732.001569.116590@g47g2000cwa.googlegr oups.com...[color=blue]
> 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.[/color]

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


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles