Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Quesiton with 'const'

51 views
Skip to first unread message

yin...@gmail.com

unread,
Feb 13, 2006, 2:33:07 PM2/13/06
to
Hi,

If I define a const stl vector attribute in my class,

class A {
public:
A();
private:
const vector<int> v;
};

can I still add/remove elements to the list outside A's constructor?

Thank you.

Gavin Deane

unread,
Feb 13, 2006, 2:44:53 PM2/13/06
to

[Best not to describe a vector as a list, since a list is a different
standard container]

No. Neither can you add or remove elements _inside_ A's constructor.
You can construct the vector is any way you choose in A's constructor's
initialisation list, but then you can't change it. That's what const
means. What did you think it meant in this context?

Gavin Deane

Victor Bazarov

unread,
Feb 13, 2006, 2:43:47 PM2/13/06
to

No. What would be the point of declaring it 'const' if you could change
it after construction?

V
--
Please remove capital As from my address when replying by mail

Greg

unread,
Feb 13, 2006, 3:27:41 PM2/13/06
to

Victor Bazarov wrote:
> yin...@gmail.com wrote:
> > If I define a const stl vector attribute in my class,
> >
> > class A {
> > public:
> > A();
> > private:
> > const vector<int> v;
> > };
> >
> > can I still add/remove elements to the list outside A's constructor?
>
> No. What would be the point of declaring it 'const' if you could change
> it after construction?
>
> V

The aim is probably to make "v" read-only in A's context, but
read/writeable in some other context.

Declaring the member vector const prevents anyone from modifying it.
However a pointer to a const object need not point to a object declared
const. So a member declaration of the form:

const vector<int>* v;

could point to a non-const std:vector<int>; all access to the vector
through v though would have to treat the vector as const.

Greg

mlimber

unread,
Feb 13, 2006, 3:51:41 PM2/13/06
to

No, as the others said, but using const and non-const member functions
with a non-const vector may give you the behavior you want:

class A
{
vector<int> v_;
public:
void Inspect() const // Note: const
{
v_.size(); // Ok: vector<>::size is also const
v_.push_back( 42 ); // Error! v_ is const in this context
}

void Modify() // Note: non-const
{
v_.size(); // Ok
v_.push_back( 42 ); // Ok
}
};

Tell us what you are trying to do, and we may be able to help more.

Cheers! --M

yin...@gmail.com

unread,
Feb 13, 2006, 3:59:11 PM2/13/06
to
Thanks. I am looking for an emulation of Java's blank final attribute
in C++.
i.e. an attribute can not be changed after it is initialized in
Constructor of the class.

Thank you for all your ideas.

Ben Pope

unread,
Feb 13, 2006, 4:06:34 PM2/13/06
to

Well a const member does that.

class withConstMember {
public:
withConstMember(const int& i) : i_(i) {}
private:
const int i_;
};

int main()
{
withConstMember w(6);
}

The only problem is that you can't initialise an array in the
initialiser list. Use std::vector<> instead.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...

Ben Pope

unread,
Feb 13, 2006, 4:10:29 PM2/13/06
to
Ben Pope wrote:
> The only problem is that you can't initialise an array in the
> initialiser list. Use std::vector<> instead.

Example using vector:


#include <vector>

class withConstMember {
public:
withConstMember(const std::vector<int>& vec) : vec_(vec) {}
private:
const std::vector<int> vec_;
};

int main()
{
// create a vector with 4 elements with value 3
std::vector<int> otherVector(4,3);

withConstMember w(otherVector);

mlimber

unread,
Feb 13, 2006, 4:31:49 PM2/13/06
to
Ben Pope wrote:
> Ben Pope wrote:
> > The only problem is that you can't initialise an array in the
> > initialiser list. Use std::vector<> instead.
>
> Example using vector:
>
>
> #include <vector>
>
> class withConstMember {
> public:
> withConstMember(const std::vector<int>& vec) : vec_(vec) {}
> private:
> const std::vector<int> vec_;
> };
>
> int main()
> {
> // create a vector with 4 elements with value 3
> std::vector<int> otherVector(4,3);
>
> withConstMember w(otherVector);
> }

Or you could do something like this using method chaining (cf.
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.18):

#include <vector>
using namespace std;

template<typename T>
class Initializer
{
vector<T> v_;
public:
Initializer& Add( const T& t )
{
v_.push_back(t);
return *this;
}

operator vector<T>() const { return v_; }
};

class A
{
public:
A()
: v_( Initializer<int>()
.Add( 86 )
.Add( 75 )
.Add( 30 )
.Add( 9 ) )
{}

private:
const vector<int> v_;
};

Cheers! --M

Andre Kostur

unread,
Feb 13, 2006, 4:27:57 PM2/13/06
to
"Greg" <gre...@pacbell.net> wrote in news:1139862461.360862.267930
@f14g2000cwb.googlegroups.com:

>
> Victor Bazarov wrote:
>> yin...@gmail.com wrote:
>> > If I define a const stl vector attribute in my class,
>> >
>> > class A {
>> > public:
>> > A();
>> > private:
>> > const vector<int> v;
>> > };
>> >
>> > can I still add/remove elements to the list outside A's constructor?
>>
>> No. What would be the point of declaring it 'const' if you could
change
>> it after construction?
>>
>> V
>
> The aim is probably to make "v" read-only in A's context, but
> read/writeable in some other context.

Huh? If A doesn't "control" v, why would v be a member?

> Declaring the member vector const prevents anyone from modifying it.
> However a pointer to a const object need not point to a object declared
> const. So a member declaration of the form:
>
> const vector<int>* v;
>
> could point to a non-const std:vector<int>; all access to the vector
> through v though would have to treat the vector as const.

Yes.... but one in an object, and one is a pointer to an object. Much
different semantics. BTW: You don't happen to be changing languages
from Java to C++, are you?

yin...@gmail.com

unread,
Feb 13, 2006, 5:05:53 PM2/13/06
to
Thanks. One question:
what does this line do?

operator vector<T>() const { return v_; }

Why we need that in the Initializer class?

Ben Pope

unread,
Feb 13, 2006, 5:11:39 PM2/13/06
to

It's a conversion to vector<T>, allowing that class to be used to
construct the member vector in the initialiser list.

yin...@gmail.com

unread,
Feb 13, 2006, 7:26:15 PM2/13/06
to
I have a different example , I wonder how can I use teh Initializer.

class A
{
public :
A(const B& b) ;
private:
const int x;
const int y;
void func1(B& b);
void func2(B& b);
}

A::A(const B&b) {
// this will not compile since this is not done in the initializer.
x = func1(b);
y = func2(b);
}

Is there a work around? of course, I can remove 'const' in x, y.
But I wonder if there is a better solution.

Thank you.

Gavin Deane

unread,
Feb 14, 2006, 3:08:11 AM2/14/06
to

yin...@gmail.com wrote:

> I have a different example , I wonder how can I use teh Initializer.
>
> class A
> {
> public :
> A(const B& b) ;
> private:
> const int x;
> const int y;
> void func1(B& b);
> void func2(B& b);
> }

Missing semicolon.

> A::A(const B&b) {
> // this will not compile since this is not done in the initializer.
> x = func1(b);
> y = func2(b);
> }
>
> Is there a work around? of course, I can remove 'const' in x, y.
> But I wonder if there is a better solution.

Removing the const qualification from x and y won't work until you
change func1 and func2 to return int instead of void and to take a
const B& instead of a B&.

After correcting those problems and simplifying the class, you need to
understand the difference between initialisation and assignment.

class B;

class A
{
public :
A(const B& b);
private:
const int x;

int func1(const B& b);
};

Do you know what the difference is between this, which does not compile

A::A(const B&b) {
x = func1(b); // Does not compile.
}

and this, which does compile

A::A(const B&b) : x(func1(b)) {} // Does compile.

Gavin Deane

Ben Pope

unread,
Feb 14, 2006, 4:54:07 AM2/14/06
to

Well none of this will work.

What do you want to achieve?

func1 and func2 do not return a value, so you can't use the return value
to initialise an int. Do func1 and func2 modify b? If not, use const,
otherwise remove the const from constructor of A.

Anyway, you can call a function in the initialiser list, consider:

class B;

class A {
public :
A(const B& b);
private:
const int x;
const int y;

int func1(const B& b);
int func2(const B& b);
};

A::A(const B& b) : x(func1(b)), y(func2(b)) {
}


With appropriate missing pieces filled in, of course.

Axter

unread,
Feb 14, 2006, 6:42:40 AM2/14/06
to
Ben Pope wrote:
> yin...@gmail.com wrote:
> > Thanks. I am looking for an emulation of Java's blank final attribute
> > in C++.
> > i.e. an attribute can not be changed after it is initialized in
> > Constructor of the class.
> >
> > Thank you for all your ideas.
>
> Well a const member does that.
>
> class withConstMember {
> public:
> withConstMember(const int& i) : i_(i) {}
> private:
> const int i_;
> };
>
> int main()
> {
> withConstMember w(6);
> }
>
>
>
> The only problem is that you can't initialise an array in the
> initialiser list. Use std::vector<> instead.

Yes, you can. All you need is a method that returns that type.
Example:
class A {
public:
A():v(Init_v()) //Initialized list
{
}
private:
vector<int> Init_v()
{
vector<int> tmp;
tmp.push_back(1);
tmp.push_back(2);
tmp.push_back(3);
return tmp;
}
const vector<int> v;
};

Moreover, you can modify the above example so that the Init_v method
takes argument(s) to assit in initializing the vector.

Ben Pope

unread,
Feb 14, 2006, 6:47:14 AM2/14/06
to
Axter wrote:

> Ben Pope wrote:
>>
>> The only problem is that you can't initialise an array in the
>> initialiser list. Use std::vector<> instead.
>
> Yes, you can. All you need is a method that returns that type.

<snip vector example>

I'll repeat, "you can't initialise an array in the initialiser list.
Use std::vector<> instead" [of an array].

0 new messages