Argument order for mixed constant and non-constant pass by reference

In accordance with practice using non-member functions, where possible to improve encapsulation , I wrote several classes that have declarations that look something like this:

void auxiliaryFunction(
        const Class& c,
        std::vector< double >& out);

Its purpose is to do something with cpublic member functions and fill the vector with output. You may notice that the order of the arguments it reminds python member function def auxiliaryFunction(self, out).

However, there are other reasonable ways of choosing the order of the arguments: it is possible to say that this feature is reminiscent of an assignment operation out = auxiliaryFunction(c). This idiom is used, for example,

char* strcpy ( char* destination, const char* source );

What if I have another function that does not look like an immaterial member function, that is, that it initializes the new object that I created:

void initializeMyObject(
    const double a,
    const std::vector<double>& b,
    MyObject& out);

So, to ensure consistency, should you use the same order (mutable last variable) as in auxiliaryFunction?

In general, is it better to choose (non-const , const)over (const, non-const)or only in certain situations? Are there any reasons for choosing one of them, or should I just choose it and stick to it?

(By the way, I know a Google style guide that suggests using pointers instead of non-constant links, but tangentially my question.)

+3
source share
2 answers

STL ( const) . ++, .

, . (.. ).

( , : , ! ...)

+4

, :

  • , . . , . , C , API . - .

  • const , , .

  • STL, , , .

  • , , ++ , , . initializeMyObject , .

  • const-, - ( , )

, :

 MyObject initializeMyObject(
    double a,
    const std::vector<double>& b,
    );

( , , std::vector<double> typedef, .)

, ( const, const) (const, const) ? - , ?

const, . - . ( ), . . , const& (const-refernces) .

+1

Source: https://habr.com/ru/post/1734150/


All Articles