How to make sure begin() const is called?

S

saneman

I am writing a class 'seq' that calls begin() and begin() const in
std::list:


template <typename V, typename A> typename seq<V,A>::iterator
seq<V,A>::seqbegin() {
return seq::list.begin();
}


// const version.
template <typename V, typename A> typename seq<V,A>::const_iterator
seq<V,A>::seqbegin() const {
return seq::list.begin();
}


But how do make sure that:

return seq::list.begin();

in 'seqbegin() const' actually returns the const_iterator from the
std::list?

Is it enough that I declare the function to return a const_iterator?
 
A

Alf P. Steinbach

* saneman:
I am writing a class 'seq' that calls begin() and begin() const in
std::list:


template <typename V, typename A> typename seq<V,A>::iterator
seq<V,A>::seqbegin() {
return seq::list.begin();
}


// const version.
template <typename V, typename A> typename seq<V,A>::const_iterator
seq<V,A>::seqbegin() const {
return seq::list.begin();
}


But how do make sure that:

return seq::list.begin();

in 'seqbegin() const' actually returns the const_iterator from the
std::list?

Is it enough that I declare the function to return a const_iterator?

This has nothing to do with templates.

When you call a member function on a const object x, you are by default
calling a const overload of that member function.

When x is a member of o, and you're calling from within a const member
function of o, x is effectively const.

struct Blah
{
void foo() {}
void foo() const {}
};

struct S
{
Blah myBlah;

void bar() { myBlah.foo(); } // Non-const foo invoked.
void bar() const { myBlah.foo(); } // Const foo invoked.
};

Cheers & hth.,

- Alf
 
G

Gianni Mariani

saneman said:
I am writing a class 'seq' that calls begin() and begin() const in
std::list:


template <typename V, typename A> typename seq<V,A>::iterator
seq<V,A>::seqbegin() {
return seq::list.begin();
}


// const version.
template <typename V, typename A> typename seq<V,A>::const_iterator
seq<V,A>::seqbegin() const {
return seq::list.begin();
}


But how do make sure that:

return seq::list.begin();

in 'seqbegin() const' actually returns the const_iterator from the
std::list?

Is it enough that I declare the function to return a const_iterator?

no.

one way is to use a const reference. i.e.

const seq::list & ref = * this;
return ref.begin();

or

return ( const_cast<const seq::list *>( this ) )->begin();
 
S

saneman

Alf said:
* saneman:

This has nothing to do with templates.

When you call a member function on a const object x, you are by default
calling a const overload of that member function.

When x is a member of o, and you're calling from within a const member
function of o, x is effectively const.

struct Blah
{
void foo() {}
void foo() const {}
};

struct S
{
Blah myBlah;

void bar() { myBlah.foo(); } // Non-const foo invoked.
void bar() const { myBlah.foo(); } // Const foo invoked.
};

Cheers & hth.,

- Alf


If I understand your example correct then my current code does the right
thing.


(1)
template <typename V, typename A> typename seq<V,A>::iterator
seq<V,A>::seqbegin() {
return seq::list.begin();
}

(2)
template <typename V, typename A> typename seq<V,A>::const_iterator
seq<V,A>::seqbegin() const {
return seq::list.begin();
}



(2) calls the const version of begin() in std::list since its declared
as 'seqbegin() const'. (1) just calls begin(). Or did I misunderstand
your example?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top