Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function that return both address and value

Tags:

c++

I want to have a single function for it. what is the best way for it? Actually I have a scenario that needs both double value and pointer to double value, to create generate another XML data message.

double* TransportMessage::getSize_Ptr()
{
   // double m_quotesize; is data member of the class
    return &m_quotesize;
}

double TransportMessage::getSize()
{
   // double m_quotesize; is data member of the class
    return m_quotesize;
}

Basically what I am looking for is, someway to just return a value and if needed fetch address out of it. I know reverse is possible and very easy, and currently I am using that way only.

I am aware of the fact that we cannot do double l_dQuote = &getSize(); so pls don't catch me on this.. :)

like image 324
Bhupesh Pant Avatar asked Feb 15 '26 08:02

Bhupesh Pant


1 Answers

Why don't you just return a reference? Then you can both read and mutate the class state (although this will cause some pain for your future maintainers - a class should be responsible for maintaining its own state, not giving the rest of the world access to it).

double& TransportMessage::get_size_ref()
{
   // double m_quotesize; is data member of the class
    return m_quotesize;
}
like image 109
Mark B Avatar answered Feb 16 '26 22:02

Mark B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!