8

Anybody think there are advantages to using a class or struct to pass arguments ?

Like instead of

f(int,float,string)

Have

f(Args)

Where Args is struct with int,float,string members.

Advantage is easy to create multiple default parameters and not have to change function signature when new arguments added.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
steviekm3
  • 905
  • 1
  • 8
  • 19

6 Answers6

5

The obvious benefit would be to have logical grouping of semantically related data items.

Once you do, add some (member) operations on the structure that will guarantee your invariants.

The encapsulation raises the abstraction level of your code and this makes it easier to maintain/reason about.

See also Law Of Demeter

sehe
  • 374,641
  • 47
  • 450
  • 633
4

I think the great advantage is not having to rely to parameter order. Rely on order is error prone if you are changing frequently the interface, instead if you change the parameter struct you are always explicitly assigning values to a member variable which has a specific semantic.

Take for example Direct3D11 ID3D11Device::CreateDepthStencilState function: passing a const D3D11_DEPTH_STENCIL_DESC *pDepthStencilDescis a lot more clear than asking for all the parameter it require.

Moreover think about modifiability: you don't need to change this method signature but only the underlying data structure during refactoring. I've found this especially useful when working collaboratively, where someone specify the interface and someone else have to implement it.

Lorenzo Belli
  • 1,767
  • 4
  • 25
  • 46
3

Anybody think there are advantages to using a class or struct to pass arguments ?

Yes, I think there's a lot of advantages.

Having large parameter lists on functions will distract client code from semantical parameter consistency, which can be better managed within an appropriate struct or class.

Also it's more flexible to use a struct, if additional (possibly optional) parameters need to be added later.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
3

Whether you use one argument contained in a class/struct or multiple arguments depends on the meanings of the arguments.

Bad use of a struct:

struct Foo
{
   char const* source;
   char* destination;
};


Foo strcpy(Foo foo);

Good use of a struct:

struct Point
{
   int x;
   int y;
};


int distanceFromOrigin(Point p) { ... }

instead of

int distanceFromOrigin(int x, int y) { ... }
R Sahu
  • 204,454
  • 14
  • 159
  • 270
3

Doing the devil advocate here. There are also drawbacks here, mostly semantical. The first and foremost, it will require a lot of code if some of those args are passed by reference, another one by constant reference, the third one by const pointer and the forth by value. Would require you to explicitly write move constructor and default constructor for the argument struct, which will quickly become tedious. It would also be tedious to add members to that struct.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
2

I also think using a struct is better: You can circumvent the hustle with parameter types and order. Suppose you have foo(A, B) (for types A and B). But still foo(b, a) might compile, depending on implicit constructions etc.

This concept can also be generalized using some kind of Context classes. Relying on C++11 variadic templates you could pass a "parameter superset context" to a subset one.

m8mble
  • 1,513
  • 1
  • 22
  • 30