473,326 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

Aug 31 '05 #1
4 3374
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
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
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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: John Harrison | last post by:
This code fails to compile on Comeau C++ and VC++ 7.1 (with language extensions disabled) template <class T> struct B { T b; }; template <class T>
5
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
8
by: dwok | last post by:
I have been wondering this for a while now. Suppose I have a class that contains some private member variables. How should I access the variables throughout the class? Should I use properties that...
5
by: Cyril Gupta | last post by:
Hello, I have a class inside another class. The Scenario is like Car->Engine, where Car is a class with a set of properties and methods and Engine is another class inside it with its own set of...
4
by: dogpuke | last post by:
I have a class CString. I'm wondering if it's possible to make a global function mystr_cat that does this: CString s1 = "hello"; s1 = mystr_cat("another", "string", "here"); Thus mystr_cat...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
6
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.