Omer
Omer

Reputation: 1

How to copy class variable and call in another file

In my main.cpp

void MainWindow::onFinish( ImageResult* result )
m_ImageIdResult = *result;
m_Result = m_ImageResult.AllMetadata;

Now I want to use the m_Result in my another display.cpp file. How do I copy the m_result so that I can use it in the other file. I tried the following as I have m_Display in header file of display.h

m_DisplayModel->howtodefinethisfunction(m_Result, asset-> );

I am sorry for this basic question, I have been unlucky with it and spent 2 days figuring it out. Thanks a lot for your time.

Upvotes: 0

Views: 61

Answers (2)

Paul Evans
Paul Evans

Reputation: 27567

The easiest way is to simply pass it as a function parameter:

some_func_in_display_cpp_file(m_Result, /* other parms ... */);

and define the function as:

return_type some_func_in_display_cpp_file(const m_result_type&, /* ... */) {

Upvotes: 2

Frecklefoot
Frecklefoot

Reputation: 1692

A function parameter is the easiest way to pass it. I'm not sure what you're doing here:

m_DisplayModel->howtodefinethisfunction(m_Result, asset-> );

For the second parameter, you seem to have a pointer pointing to nothing. That will give you a compile error. But if you want the exact syntax of how to define the function, if you have m_Result declared in the MainWindow class like this:

CoolResult m_Result;

Then you would declared the function in DisplayModel like this:

public:
void DoSomethingWithResult( CoolResult result );

Then you would call it from your MainWindow class like this:

void MainWindow::onFinish( ImageResult* result )
m_ImageIdResult = *result;
m_Result = m_ImageResult.AllMetadata;

// this will probably be declared somewhere else in real code
DisplayModel display;
display.DoSomethingWithResult( m_Result );

But if what you want is to get result in DisplayModel from MainWindow, you'll create a getter in MainWindow (MainWindow.h):

public:
CoolResult GetResult();

MainWindow.cpp:

CoolResult MainWindow::GetResult()
{
    return m_Result;
}

Hope this helps. If this doesn't cover your question, please include more details.

Upvotes: 0

Related Questions