Indexing an array a[] with a+1
Maybe you can help me with a small question. It is kind of hard to explain, but I made a little code example that I hope explains my thoughts.
I'm thinking of the code that calls the print function with the second element in the array: print(a+1);
What I think I'm doing is that I sends an pointer to a[1] into the function print.
It seems to works as I intended but is it safe to do it this way? (Nevermind that it is not the simplest or prefered way to do this.) Also, if the class Something is an subclass to another class, does it change anything?
Code:
#include <iostream>
using namespace std;
class Something
{
public:
Something(int in = 0){ n = in; };
void setData(int in) { n = in; };
int getData() { return n; };
protected:
int n;
};
void print(Something *in)
{
cout << in->getData() << endl;
}
int main()
{
Something a[5];
a[0].setData(1);
a[1].setData(2);
a[2].setData(3);
print(a+1);
char wait;
cin.get(wait);
}
Re: Indexing an array a[] with a+1
Quote:
Originally Posted by Suzi
What I think I'm doing is that I sends an pointer to a[1] into the function print.
Yes, that is correct and it is safe.
a+1 is the same as &a[1].
:)
Re: Indexing an array a[] with a+1
Maybe it's just me but I feel that &a[1] is less obscure though so in this context I would have used that.
Re: Indexing an array a[] with a+1
Better:
Code:
void print(Something& in)
{
cout << in.getData() << endl;
}
int main()
{
Something a[5];
a[0].setData(1);
a[1].setData(2);
a[2].setData(3);
print(a[1]);
}
Even better:
Code:
class Something
{
public:
Something(int in = 0){ n = in; };
void setData(int in) { n = in; };
int getData() const{ return n; };
protected:
int n;
};
void print(const Something& in)
{
cout << in.getData() << endl;
}
If you are not intending to pass NULL (and you are not checking for it inside the function), use references, not pointers. And learn about const correctness ;-)
Re: Indexing an array a[] with a+1
Might want to overload the ostream& operator<<(ostream&, const Something&) so that you could directly do:
Code:
std::cout << *(a+1);
//or
std::cout << a[1];
Re: Indexing an array a[] with a+1
Quote:
Originally Posted by S_M_A
Maybe it's just me but I feel that &a[1] is less obscure though so in this context I would have used that.
nope. it's not just you. I agree. print(a+1) is vague. It's not obvious that you are passing a pointer to an object just by looking at the code. In a really large program that would be a bad coding technique and would result in software that is difficult to read and maintain.
The suggestion to overload stream operator was really good as well as treuss' suggestion to use references rather than pointers. Pointers can be a pain because they could be set to NULL. If you pass a lot of pointers to things through functions, then you have to validate them somehow so that you don't end up with undefined run time behavior. The function taking a pointer has no idea if the caller set the value correctly.
Re: Indexing an array a[] with a+1
Hey,
thanks all for your answers!
Lots of great suggestions, I know that my example isn´t the best but I only made it to demonstrate my question. It´s not something I would use in a real project but I´m really thankful for all your help and ideas to make my code better :)
/Suzi