Quesiton with 'const'

Y

yinglcs

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.
 
V

Victor Bazarov

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
 
G

Gavin Deane

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?

[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
 
G

Greg

Victor said:
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
 
M

mlimber

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.

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
 
Y

yinglcs

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.
 
B

Ben Pope

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.

Ben Pope
 
B

Ben Pope

Ben said:
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);
}



Ben Pope
 
A

Andre Kostur

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?
 
M

mlimber

Ben said:
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
 
Y

yinglcs

Thanks. One question:
what does this line do?
operator vector<T>() const { return v_; }

Why we need that in the Initializer class?
 
B

Ben Pope

Thanks. One question:
what does this line do?
operator vector<T>() const { return v_; }

Why we need that in the Initializer class?

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

Ben Pope
 
Y

yinglcs

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.
 
G

Gavin Deane

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
 
B

Ben Pope

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.

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.

Ben Pope
 
A

Axter

Ben said:
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.
 
B

Ben Pope

Axter said:
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].

Ben Pope
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top