0

I really cannot decide what option is the best, I see plenty of down/upsides to both approaches and right now I'm undecided.

For example;

class DX11GBuffer
{
public:
    DX11GBuffer(ID3D11DevicePtr device, ID3D11DeviceContextPtr context, D3D11_TEXTURE2D_DESC backbufferTextureDesc);
    ~DX11GBuffer();

    void BindForGeometryStage(ID3D11DepthStencilViewPtr dsv);
    // ...
}

"dsv" is not owned by the object and thus passed every frame, but it could just aswell be passed as a argument in the constructor and stored seeing as it is a smart pointer.

I guess what I am getting at is I understand there are pitfalls for keeping too much state in an object but also passing so much as function arguments means a whole lot of ASCII. Likewise defining "argument objects", collection of data for functions, seems to add alot of overhead aswell if it is to be applied everywhere as a rule of thumb.

I guess I am looking some general guidelines on when to pass as argument vs when to store as member.

3
  • 1
    Dear God, this is disgusting. Protip: Those typedefs hide way more than they show, and you should not typically be implementing your own classes that have destructors. – DeadMG Dec 25 '14 at 14:55
  • Could you elaborate? – KaiserJohaan Dec 25 '14 at 16:52
  • @DeadMG: these types are COM smart pointers (COM means "component object model", referring to a specific Microsoft technology for C++ interoperability within Windows). In particular the code excerpt makes use of Microsoft Direct3D version 11. COM smart pointers have the typical properties of reference-counted smart pointers. Using COM smart pointers in argument passing, as C++ class members, etc., are RAII-safe, as long as one fully understands the existence of two worlds - the COM part and the standard C++/C++11 part. The difficult part is in defining one's own COM class but OP is not doing. – rwong Dec 25 '14 at 17:05
2

Member variables are variables are a "has a" relationship (the rectangle has a width). If the object does not have the variable then it should be passed into it to be used where required, if it's related to the object itself then it should be stored in the object.

A quick way to test the relation could be seeing how much that object uses the variable. If you use it in just one unit of code, leave it as it is (being passed in) however if multiple functions within that object make use of it then storing it as a member variable would probably make more sense.

2
  • 3
    A better test is: while the object is not changing its state, will there be different values of this variable used? If so, pass it as an argument. If not, chances are very high it should be part of the objects internal state. – valenterry Dec 25 '14 at 20:55
  • @valenterry +1, I'd agree with that! A more accurate way of what I attempted to say. Would you mind if I edit that in for clarity and correctness of the answer? – Elliot Blackburn Dec 25 '14 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.