constant array of constant objects?
Mar 1, 2014 at 5:18pm UTC
The following example works but nothing is constant.
bPtrs[] is an array of pointers to BClass objects.
How to make bPtrs[] constant and the BClass objects constant?
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
#include <iostream>
using namespace std;
class BClass
{
private :
char code; //how to make const?
public :
void setCode(char c) { code = c; }
char getCode() { return code; }
};
class AClass
{
private :
BClass b0; //how to make const?
BClass b1; //how to make const?
BClass* bPtrs[];//how to make const?
public :
AClass()
{
b0.setCode('x' );
b1.setCode('y' );
bPtrs[0] = &b0;
bPtrs[1] = &b1;
}
void print(int i)
{
cout << "char=" << bPtrs[i]->getCode() << endl;
}
};
AClass a;
int main()
{
a.print(0); // prints "char=x"
a.print(1); // prints "char=y"
}
I considered using static, by reference, and constructor initialization list; but not sure which one would work in this example.
Thank you.
Mar 1, 2014 at 6:17pm UTC
Are the class members really const? Or is it just the interface that needs changing?
Foe example, what would you expect to do with
a
and
ca
if they were declared as:
1 2
AClass a;
const AClass ca;
Mar 1, 2014 at 6:26pm UTC
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
#include <iostream>
using namespace std;
class BClass
{
private :
const char code; //how to make const?
public :
BClass(char c) : code(c) {}
char getCode() const { return code; }
};
class AClass
{
private :
const BClass b0; //how to make const?
const BClass b1; //how to make const?
const BClass* bPtrs[2];//how to make const?
public :
AClass() : b0('x' ), b1('y' )
{
bPtrs[0] = &b0;
bPtrs[1] = &b1;
}
void print(int i) const
{
cout << "char=" << bPtrs[i]->getCode() << endl;
}
};
AClass a;
int main()
{
a.print(0); // prints "char=x"
a.print(1); // prints "char=y"
}
http://ideone.com/elKVjp
Mar 1, 2014 at 6:44pm UTC
kbw,
Thank you for the quick response.
What do mean by "interface change"?
What does "const AClass ca;" do?
None of the data in AClass or BClass changes.
There is only one instance of AClass.
AClass is composed of BClass objects.
Mar 1, 2014 at 6:49pm UTC
There's a difference between an instance of a constant object and and object made up of const members.
For example:
1 2 3
const std::string name = getName();
name = "new name" ; // can't do that.
size_t name_length = name.size(); // this is ok, size() is a const method
This different from an object that is mutable, but has immutable members.
So the question is, which of these do you want?
Last edited on Mar 1, 2014 at 6:50pm UTC
Mar 1, 2014 at 7:25pm UTC
I want both: a constant object made up of constant members.
None of the data in AClass nor BClass will change.
AClass object will always contain the same BClass objects.
Mar 1, 2014 at 8:09pm UTC
I want both: a constant object made up of constant members.
Well, if that's the case, just declare the members as const.
They have to be initialised in the initialiser list on construction and the objects cannot be assigned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
class AClass
{
private :
const BClass b0;
const BClass b1;
const BClass** bPtrs;
public :
AClass()
{
// You can't do these because b0, b1 and bPtrs are const and already initialised
b0.setCode('x' );
b1.setCode('y' );
bPtrs[0] = &b0;
bPtrs[1] = &b1;
}
void print(int i) const
{
cout << "char=" << bPtrs[i]->getCode() << endl;
}
}
AClass object will always contain the same BClass objects.
This isn't a const thing, it's static membership.
Although these things can be done, I am wondering if you're approaching your problem in a conventional way.
Mar 1, 2014 at 8:59pm UTC
Thanks cire. That's perfect.
Topic archived. No new replies allowed.