Hi,

I'm making a program using Visual Studio 2010. I'm strugging with how to correctly use classes and their instances.

For example; What is the difference between using the following ways to call function from one class within another...

Example 1:
Dialog.h

#include OtherClass.h
    MyOtherClass myClass;

Dialog.cpp

 myClass.MyFunction();

Example 2:
Dialog.h

#include OtherClass.h
    MyOtherClass *myClass;

Dialog.cpp

myClass->MyFunction();

What I am finding difficult is how to call functions of a class from multiple other classes without creating new instances in each one. E.g. I create an instance of a serial port class in my dialoge class, do some comminication, then some code in another class wants to do some serial coms, but it can't because I can only acess the port in the original instance of the serial class.

I hope my questions make some sense, I'm really newbie to the object oriented stuff.

Do you mean inheritence?

Inheriting methods from one class to another.

Possibly. Does inheritence work differently between my two examples?

It sounds like you need to pass the serial comms object (as a reference or pointer) into the functions that need to use it.

So is that what doing it like this does...?

myClass->MyFunction();

MyOtherClass myClass;

The above declares an object of class MyOtherClass.

myClass.MyFunction();

The above calls a member function called MyFunction() on the object called myClass.

MyOtherClass *myClass;

The above declares a pointer to an object of class MyOtherClass. In other words, the pointer can store the address in memory of an object of class MyOtherClass.

myClass->MyFunction();

The above calls a member function called MyFunction() on the object pointed to by the pointer myClass.

What I am finding difficult is how to call functions of a class from multiple other classes without creating new instances in each one.

What you need for this is to share the instance (object) between the different objects that need it. This can be done with pointers (or better, with a std::shared_ptr).

So is that what doing it like this does...?

myClass->MyFunction();

No, this is calling MyFunction on a pointer to a MyOtherClass object.

If your serial port class was called SerialPort and you have created an instance sp in you function then you can call methods of the SerialPort class by passing sp into other functions as an argument:

void SomeFunction()
{
    // Create an instance of the serial port class
    SerialPort sp;

    /* Do some things */

    // Call some communication function on the serial port object
    sp.DoComms();

    /* Do some more things */

    // Pass the serial port object into this function, as it needs it for something
    SomeOtherFunction( sp );

    /* Maybe do even more things */
}

/// This function accepts a refernce to a SerialPort object as an argument so
/// that it can use it to perform communications on the port
void SomeOtherFunction( SerialPort& port )
{
    /* Do somthing */

    // Call some communication function on the serial port object
    // This is the exact same object as in the other function (not
    // a copy or clone of it)
    port.DoComms();

    /* Finish doing stuff */
}

By creating your SerialPort one function and passing it to another, you can be using the same one in each function.

Is that roughly what you're trying to do?

Thanks, that makes sense. I'm not sure I can pass sp like this in my program though because I'm using a timer..

I have a MainDlg.cpp which will call functions of DataProcess.cpp when a button is pressed for example

CDataProcess dataProc;
dataProc.SystemActivation()

SystemActivation will open a serial port and do some comms using a serial port class, e.g.
DataProcess.cpp

CSerial *sp;

SystemActivation() {
    sp->OpenPortAndSendStuff("COM1");
    return true;
}

There's also a file ChildView.cpp which holds a Timer function that is called by ON_WM_TIMER() message

CDataProc *dataProc;

OnTimer(UINT_PTR nIDEvent)
{   
    // How can I get/pass the serial handle from here?
    dataProc->DoTimerStuff();
}

DoTimerStuff will call functions in CDataProc class that in turn does some serial stuff. The problem with this is that in the timer, this other instance of dataProc (and therefore CSerial?) causes an Unhandled Exeption/ Access Violation.
Perhaps my approach is just wrong. If the timer were in CDatatProcess, it would be ok, but this class wont get any ON_WM_TIMER() events becasue it is not a dialog. (I think). Any suggestions?

If the CSerial object is something that will need to be used by many different classes then you basically have three options.

  • Pass the pointer/reference to a CSerial object to all the methods that need it (mentioned above)
  • Have a pointer/reference to a CSerial object as a member variable of the classes that need it
  • Have a global CSerial instance that you access through some kind of GetSerialInstance function

These options are roughly in order of how desirable I'd find them as solutions to a problem (with the global variable option being the least desirable). From your description, it's not that clear what the structure of your program is, or how it could be re-structured to use any of these approaches.

Also, if you have the possibility that many functions will try to access the same CSerial object at the same time (for example, in a multi-threaded program) then you should also probably worry about locking the CSerial object when writing out data and such.

Thank you. Took me a while to get my head round it, but this is what I've done and it sems to work so far! :)

I couldn't pass the pointer of CDataProc to the OnTimer method becasue of it being called by an event. So what I did was create a function in the CChildView class (which contains the timer) to get the pointer to the CDataProc object when it loads up. So in the CMainDialog class, I call PassHandles(*dataProc);

This seems to all be working now and I don't intend on making any more instances of CDataProc or CSerial, so It should be ok. Hopefully I understand enough now to pass pointers around if I need to, and without breaking my program!

Thank you for the help. Much appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.