Bhupesh Pant
Bhupesh Pant

Reputation: 4349

Function that return both address and value

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.. :)

Upvotes: 3

Views: 128

Answers (1)

Mark B
Mark B

Reputation: 96281

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;
}

Upvotes: 12

Related Questions