473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Quesiton with 'const'

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.

Feb 13 '06 #1
16 1693

ying...@gmail.com wrote:
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

Feb 13 '06 #2
yi*****@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
--
Please remove capital As from my address when replying by mail
Feb 13 '06 #3

Victor Bazarov wrote:
yi*****@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

Feb 13 '06 #4
yi*****@gmail.com wrote:
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

Feb 13 '06 #5
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.

Feb 13 '06 #6
yi*****@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.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 13 '06 #7
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);
}

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 13 '06 #8
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-lit...ml#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

Feb 13 '06 #9
"Greg" <gr****@pacbell.net> wrote in news:1139862461.360862.267930
@f14g2000cwb.googlegroups.com:

Victor Bazarov wrote:
yi*****@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?
Feb 13 '06 #10
Thanks. One question:
what does this line do?
operator vector<T>() const { return v_; }

Why we need that in the Initializer class?

Feb 13 '06 #11
yi*****@gmail.com wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 13 '06 #12
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.

Feb 14 '06 #13

yi*****@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

Feb 14 '06 #14
yi*****@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);
}

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
--
I'm not just a number. To many, I'm known as a string...
Feb 14 '06 #15
Ben Pope wrote:
yi*****@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.

Feb 14 '06 #16
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].

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 14 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
8
by: Sergey Tolstov | last post by:
Hello, I am working with Visual C++ 6.0 compiler. In the following declaration: int const A = 10, B = 10; both A and B are const. However, in declaration
20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
4
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.