I want two function names that do the same thing, allowing the user to use whichever they prefer.

For Vec a, i use "const" because a is never changed. Correct me if this is the wrong usage.

For int n in operator|, i do NOT use "const" because n is altered.

So my question is, should I use "const int n" in stack, because technically n doesn't change in stack, even though it changes in operator|.

Mat operator|( const Vec a, int n ){ // puts n horizontal a's next ontop of each other
	Mat x;
	for(; n>0; --n ) x.push_back(a);
return x;
} // so if n=0, we return an empty matrix

Mat stack( const Vec a, const int n ){ return a|n; }

Recommended Answers

All 8 Replies

Also, should I use "const" within a function for a variable which will not change?

Mat concat( const Vec a, const Mat b ){ // outputs vertical vector and matrix to right
	short n = a.size();
	short l = b[0].size(); // number of rows
	Mat cat(n);
	Vec row(l+1); // we need an extra row for the vertical vector
	for( short r=0; r<n; ++r ){
		row[0] = a[r];
		for( short c=0; c<l; ++c ) row[c+1] = b[r][c];
		cat[r] = row;
	}
return cat;
}

In this case, I am asking if I should be using "const short n" and "const short l" instead of what I have.

For Vec a, i use "const" because a is never changed. Correct me if this is the wrong usage.

a is passed by value, so you're already working with a copy. While it's not wrong to pass by const value, it's not very common. The reason I think your parameter is wrong, is because a should be passed by reference for performance reasons. Calling the copy constructor to pass by value can slow things down drastically:

Mat operator|(const Vec& a, int n);

Now a is passed by const reference, which gives you the performance benefits of passing by reference without the risk of modifying the original object.

So my question is, should I use "const int n" in stack, because technically n doesn't change in stack, even though it changes in operator|.

I wouldn't bother, but passing by const value is a somewhat controversial topic. Pick whichever option you find most intuitive.

Also, should I use "const" within a function for a variable which will not change?

Once again, use your own judgment on what should be const in these cases. Eventually you'll have enough experience to know where the risk of modifying something that should change is enough to justify making it const and where it's unnecessary.

Thank you. Before I mark this thread as solved, let me get this straight:

Mat operator|(const Vec& a, int n);

Now a is passed by const reference, which gives you the performance benefits of passing by reference without the risk of modifying the original object.

You are saying that "operator|(const Vec& a, int n)" has performance benefits over "operator|( Vec a, int n)", correct?

Thanks!

You are saying that "operator|(const Vec& a, int n)" has performance benefits over "operator|( Vec a, int n)", correct?

Yes. The former passes a reference while the latter calls the copy constructor to make a copy of the Vec object. With a class like Vec (or most user-defined types), I don't imagine that passing by value would be faster than by reference.

Yes Beacuse when you pass by reference you dont need to copy the entire vector like you do when you pass it by value. if you had a vector of 1000 objects each 10 bytes in size then to pass it by value you would have to copy all 10kb worth of data. passing by reference you copy 10 bytes.

Thanks! So now I know what to do with my Vec passing.

Can I pass shorts and ints by reference also? Would it look like

Mat newfunction(const Vec& a, const int& n);

?

And of course, is it WORTH passing ints by reference?

>>And of course, is it WORTH passing ints by reference?
No not really, unless you do need to modify the int. Reference and ints usually take up same size, so no noticeable advantage.

>>And of course, is it WORTH passing ints by reference?

Certainly not by const-reference. Ints, like pointers and references, are usually of the native word size of the computer, which is 32bit or 64bit on most PCs. So, making a new int variable (i.e. copying it) and passing it by value is not going to be more expensive than creating a new pointer or reference, storing the address of the int variable that was given as argument to the function, and passing that pointer or reference into the function. What could potentially be slower (depending on how clever your compiler is) is that every use of the variable within your function will require a dereference (explicitly when using a pointer, or implicitly when using a reference), this is an additional step that could slow things down a bit, however, compilers tend to be pretty clever at avoiding this if there's a better way.

About passing by const value, I guess you could say it's a bit controversial whether there is any benefit to it. But, I have been pretty satisfied with the arguments on the side of "it doesn't make the slightest difference", so I hold that opinion. The main argument is that a compiler can easily see, within a function, if you modify the value or not, as part of the basic optimizations it does, so marking as const isn't going to tell the compiler anything that it doesn't know already, so no optimization opportunity is created with the addition of "const" on a pass-by-value parameter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.