Using / returning an iterator

Hi all,

I just read the iterator reference at cplusplus.com and a chapter about iterators in a book, but I still cannot get the point about them.

Here is my code snippet followed by the question.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iterator>
#include <string>

using namespace std;

class Status
{
	private:
		bool stat;
		
	public:
		Status()
		{
			stat = false;
		}
		
		bool getStatus() const
		{
			return stat;
		}
		
		void setStatus(bool stat)
		{
			this->stat = stat;
		}
}


class myClass
{
	private:
		Status stat;
	
	public:
		myClass(){};
		
		Status& getStatus() const
		{
			return stat;
		}
}


int main(void)
{
	myClass var();
	
	var.getStatus().getStatus();
}


In myClass I would like to return a reference of the Status class. Because of the const in line 37 the code does not work.

That is the point where I would like to use an iterator. I just dont't know how. I got to the point to use the iterator template, but that is it.

 
iterator<Status, input_iterator_tag> it_Status;


But what shall I do after that? How can actually return an iterator and access the methods of the Status class.

It would be great if some of you could help me on that topic.
Last edited on
Iterators are for containers, this doesn't look like it would apply here. You could try returning a pointer if you want.
Why do you want to use iterators here? It sounds like you don't understand what iterators are for. Iterators are used to iterate over the elements in a container.

Last edited on
You should write

1
2
3
4
		const Status& getStatus() const
		{
			return stat;
		}


and in main

var.getStatus().Status::getStatus();

Also you shall place ; after a class definition.

And as for iterators they are used with containers or sequences. You need no iterator in your code.
Last edited on
@vlad what is the advantage of doing var.getStatus().Status::getStatus();
compared to just var.getStatus().getStatus();?
Last edited on
There a couple of simple solutions to your current problem and like the others said you don't need an iterator here. Because you are returning a reference and you have declared getStatus as const, it automatically returns the Status& as a const and it is complaining that it cannot convert from const Status& to Status&. You can either change the return value to be a const Status& or const cast away the constness (In your case I would not do).

In other words, when you call a const member function, whether the object is const or not, it treats it as if this is const, you can verify this by calling a const member function from a non cost object and looking at the 'type' of the 'this' pointer, it will be a const * to a const 'whatever my object type is'

As a side note: Do you really want to return a static reference here in the first place? By returning a reference you are now exposing your private data that is Status. If what you really want is just to get the "status' status" then might I suggest this alternative, still adhering to the encapsulation of MyClass.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class myClass
{
   //... Stuff in myClass
    bool getStatus() const
    {
      return stat.getStatus();
    }
   //...
}

// Now main would look like this...

main()
{
  myClass var;
  var.getStatus();
}
Last edited on
@vlad what is the advantage of doing var.getStatus().Status::getStatus();
compared to just var.getStatus().getStatus();?


Because it is a bad idea to use the same name for two functions defined in class and in its member. So for selfdocumentation i inserted class name before the second call of the function.
Last edited on
Many thanks to all of you.

@clanmjc:
I know that I am exposing private date here. But this is just an example.
Topic archived. No new replies allowed.