Archived

This topic is now archived and is closed to further replies.

Amsok

C++ question

Recommended Posts

Amsok    122
Hello, I have a small question relating C++. If 3 classes, one called: "Main" is in Main.cpp, "Other" is in Other.cpp, "Cool" is in Cool.cpp. Lets assume I make an instance of the class "Other" inside Main.cpp called "otherInstance" and call a method doSomething() on that instance ( otherInstance.doSomething() ). How can I call that exact method, on that exact instance of the class from Cool.cpp ? I apologize if my "lingo" might be off, but i just migrated from java thanks, Amsok

Share this post


Link to post
Share on other sites
malpass    122
if its the same in c++ as C# (cant remember, havent done C++ in months) then it should be the exact one anyway, if not the make it static by putting static in front and call it with Other.doSomething() inside the main()

-------------------------------------------------
Edit: Oh soz, read the question backwards. Nvmnd

[edited by - Malpass on April 30, 2003 2:24:52 PM]

Share this post


Link to post
Share on other sites
Sneftel    1788
If otherInstance is a global variable, then in other files that need to access it, declare it as extern (just like you declared it in main.cc, but with an ''extern'' on the front). This tells the compiler to look for it in other files.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
JimH    122
Offhand, I can think of two ways you would do that. The first is to make the instance of otherInstance visible from within the Cool.cpp file. The second is to pass a pointer or reference to otherInstance to the Cool object.

Here’s an example of the first. I didn’t check this with a compiler, so it might have mistakes.


      
///////////////

//Main.cpp

///////////////

#include “Other.h”

Other otherInstance;
main()
{
}

///////////////

//Other.h

///////////////

class Other
{
public:
void doSomething(void){};

};


///////////////

//Cool.h

///////////////

class Cool
{
public:
void callDoSomething(void)
};


///////////////

//Cool.cpp

///////////////

#include “Cool.h”
#include “Other.h”

extern Other otherInstance; // this makes otherInstance visible to the rest of the Cool.cpp file.


void Cool::callDoSomething(void)
{
otherInstance.doSomething();
}


If otherInstance is a pointer and the object is allocated on the heap with new, then the extern declaration would be:

extern Other* otherInstance;

and naturally you would call it with:

otherInstance->doSomething();


In the second way, you can pass a reference (or a pointer) to the Cool object:

  

///////////////

// Main.cpp

///////////////

#include “Other.h”
#include “Cool.h”

Other otherInstance;
Cool cool;
main()
{
cool.callDoSomething(otherInstance);
}


///////////////

//Other.h

///////////////

class Other
{
public:
void doSomething(void){};

};


///////////////

//Cool.h

///////////////

class Cool
{
public:
void callDoSomething(Other& other);
};


///////////////

//Cool.cpp

///////////////

#include “Cool.h”
#include “Other.h”

// No extern declaration needed.


void Cool::callDoSomething(Other& other)
{
other.doSomething();
}


Edit:
Oops. Somewhere along the way, my post lost a couple of source markers. So the second source section got mixed into the first and probably missed. So now it's fixed. My second source section is the same method as petewood has below.

[edited by - JimH on May 1, 2003 1:40:03 PM]

Share this post


Link to post
Share on other sites
petewood    819

  
//Cool.h

class Cool {
public:
void someFunction(Other& other);
};
//Cool.cpp

void Cool::Cool(Other& other) {
other.doSomething();
}
//Other.h

class Other {
public:
void doSomething()
};


  
//main.cpp

#include "Cool.h"
#include "Other.h"

int main() {
Cool coolInstance;
Other otherInstance;
coolInstance.someFuntion(otherInstance);
return 0;
}


[edited by - petewood on May 1, 2003 4:19:50 AM]

Share this post


Link to post
Share on other sites