csguy
csguy

Reputation: 1484

Pass a pointer by const reference

Is passing a pointer by const reference not done for optimization?

Ex.

bool testHelper(const TreeNode*& p, const TreeNode*& q) {
    return false;
}

TreeNode* test(TreeNode* root, TreeNode* p, TreeNode* q) {
    recursiveHelper(p, q);
}

Error:

Line 17: Char 28: error: binding reference of type 'const TreeNode *' to value of type 'TreeNode *' not permitted due to incompatible qualifiers
                testHelper(p, q);
                           ^
Line 12: Char 42: note: passing argument to parameter 'p' here
        bool testHelper(const TreeNode*& p, const TreeNode*& q) {
                                         ^

Upvotes: 0

Views: 167

Answers (1)

eerorika
eerorika

Reputation: 238491

Is passing a pointer by const reference not done for optimization?

No, it isn't, because it isn't any faster. The added indirection is potentially slower.

bool testHelper(const TreeNode*& p, const TreeNode*& q) 

Those are not references to const. Those are references to non-const pointers to const.


Other bugs:

  • test was declared to return non-void but lacks a return statement
  • You haven't declared the function recursiveHelper that you call.

Upvotes: 2

Related Questions