I have C++ code that boils down to something like the following:
class Foo{
bool bar;
bool baz;
Foo(const void*);
};
Foo::Foo(const void* ptr){
const struct my_struct* s = complex_method(ptr);
bar = calculate_bar(s);
baz = calculate_baz(s);
}
Semantically, the bar and baz member variables should be const, since they should not change after initialization. However, it seems that in order to make them so, I would need to initialize them in an initialization list rather than assign them. To be clear, I understand why I need to do this. The problem is, I can't seem to find any way to convert the code into an initialization list without doing one of the following undesirable things:
Call complex_method twice (would be bad for performance)
Add the pointer to the Foo class (would make the class size needlessly large)
Is there any way to make the variables const while avoiding these undesirable situations?
If you can afford a C++11 compiler, consider delegating constructors:
class Foo
{
// ...
bool const bar;
bool const baz;
Foo(void const*);
// ...
Foo(my_struct const* s); // Possibly private
};
Foo::Foo(void const* ptr)
: Foo{complex_method(ptr)}
{
}
// ...
Foo::Foo(my_struct const* s)
: bar{calculate_bar(s)}
, baz{calculate_baz(s)}
{
}
As a general advice, be careful declaring your data members as const, because this makes your class impossible to copy-assign and move-assign. If your class is supposed to be used with value semantics, those operations become desirable. If that's not the case, you can disregard this note.
One option is a C++11 delegating constructor, as discussed in other answers. The C++03-compatible method is to use a subobject:
class Foo{
struct subobject {
const bool bar;
const bool baz;
subobject(const struct my_struct* s)
: bar(calculate_bar(s))
, baz(calculate_baz(s))
{}
} subobject;
Foo(const void*);
};
Foo::Foo(const void* ptr)
: subobject(complex_method(ptr))
{}
You can make bar and baz const, or make the subobject const, or both.
If you make only subobject const, then you can calculate complex_method and assign to bar and baz within the constructor of subobject:
class Foo{
const struct subobject {
bool bar;
bool baz;
subobject(const void*);
} subobject;
Foo(const void*);
};
Foo::Foo(const void* ptr)
: subobject(ptr)
{}
Foo::subobject::subobject(const void* ptr){
const struct my_struct* s = complex_method(ptr);
bar = calculate_bar(s);
baz = calculate_baz(s);
}
The reason that you can't mutate const members within a constructor body is that a constructor body is treated just like any other member function body, for consistency. Note that you can move code from a constructor into a member function for refactoring, and the factored-out member function doesn't need any special treatment.
You may use delegate constructor in C++11:
class Foo{
public:
Foo(const void* ptr) : Foo(complex_method(ptr)) {}
private:
Foo(const my_struct* s) : bar(calculate_bar(s)), baz(calculate_baz(s)) {}
private:
const bool bar;
const bool baz;
};
If you don't want to use the newfangled delegating constructors (I still have to deal with compiler versions that don't know about them), and you don't want to change the layout of your class, you could opt for a solution that replaces the constructor with const void * argument by a static member function returning Foo, while having a private constructor that takes the output from complex_method as argument (that latter much like the delegating constructor examples). The static member function then does the necessary preliminary computation involving complex_method, and ends with return Foo(s);. This does require that the class have an accessible copy constructor, even though its call (in the return statement) can most probably be elided.
Related
Suppose I have a class with some constant member:
class MyClass {
public:
MyClass(int a) : a(a) {
}
MyClass() : MyClass(0) {
}
~MyClass() {
}
const int a;
};
Now I want to store an instance of MyClass somewhere, e.g. as a global variable or as an attribute of another object.
MyClass var;
Later, I want to assign a value to var:
var = MyClass(5);
Obviously, this does not work, because the assign operator is invoked, which does not exist by default, because MyClass has a const attribute. So far so good.
My question is, how can I assign a value to var anyway? After all, var itself is not declared to be a constant.
My thoughts so far
I know that the problem does not exist if I use a pointer for var:
MyClass *var;
var = new MyClass(5);
However, I would not like to use a pointer for convenience reasons.
A potential solution is to overwrite the memory with placement new:
template<class T, class... Args>
T &emplaceVar(T &myVar, Args&&... args) {
myVar.~T(); // free internal memory
return *new (&myVar) T(args...);
}
emplaceVar(var, 5);
This would solve the problem, but I am not sure if this may cause memory leaks or any other issues I have not thought of due to my lack of experience in c++. Furthermore, I would have thought there must be an easier way. Is there?
const members are problematic in general for the very reason you discovered.
The much simpler alternative is to make the member private and take care to provide no means to modify it from outside the class:
class MyClass {
public:
MyClass(int a) : a(a) {
}
MyClass() : MyClass(0) {
}
~MyClass() {
}
private:
int a;
};
I did not add a getter yet, because you say access via myObject.a is a hard requirement. Enabling this requires a bit of boilerplate, but it is much less hacky than modifiying something that must not be modified:
class MyClass {
public:
struct property {
const int& value;
operator int(){ return value;}
property(const property&) = delete;
};
MyClass(int a = 0) : value(a) {}
private:
int value;
public:
property a{value};
};
int main(){
MyClass myObject{5};
int x = myObject.a;
//myObject.a = 42; // error
//auto y = myObject.a; // unexpected type :/
}
Live Demo
Drawback is that it does not play well with auto. If by any means you can accept myObject.a() I would suggest to use that and keep it simple.
how can I assign a value to var anyway?
You can do that with a user-defined assignment operator:
class MyClass {
public:
MyClass &operator=(const MyClass &o)
{
// Implement your assignment here
return *this;
}
// ...
};
Your assignment operator can do anything that any operator= overload can. The only thing it can't do is assign anything to its const class member. That's because it's constant.
If a class does not have user-defined assignment operator, the default assignment operator assigns each member of the assigned-to object from the same member of the assigned-from object. However the default assignment operator is deleted from any class that has a const member, because that, of course, is no longer possible.
In your user-defined operator you can do whatever it means to assign one of these objects from another one. The only thing it can't do is the same thing any other class method can't do: modify a const class member.
You mentioned manual invocation of a destructor and placement new. That's possible, provided that all requisite requirements are met and undefined behavior is carefully avoided. However, technically, it wouldn't be assignment, but rather a manual destruction and construction of another object.
I have a class which is used in a union, and therefore cannot have a (non-trivial) constructor. I need to create a const instance of the class, can this be done?
i.e.:
class Foo {
// no constructors...
private:
int i;
};
union {
Foo foo;
Bar bar;
} FooBar;
const Foo defaultFoo = ???
Yes, you can copy-construct it from the result of a function:
static Foo configureDefaultFoo()
{
Foo f; // not const
f.setI(42); // call non-const member functions
return f;
}
const Foo defaultFoo = configureDefaultFoo();
Note that although this results in an object that is const, it is dynamic initialization not static, and so it can suffer from the static initialization order fiasco (same would be true if calling a non-trivial constructor, only aggregate initialization would avoid the fiasco).
I've searched high and low for the answer for this, perhaps I'm just not using the right terms to get any results?
Is there any way to make it so that a member variable is const in that it can't be reassigned, and will always be the same object, but still allow the object itself to be modified? Much like the behavior of a const pointer to a non-const object, but without being an actual pointer?
The main use case that I see for this would be composition. Let's say Foo has-a Bar, and you want to be able to access and modify that Bar, but not change which Bar Foo has. Just change the properties/call non-const methods on that Bar. Is there any way to do this?
Not with const correctness machinery; it's too primitive for that (it's just a single bit: either "change" or "not change").
You can however mark assignment private and the container a friend so that only container methods will be allowed to assign, but mutators could be marked public for others to use.
class Foo {
public:
int x, y;
Foo() : x(0), y(0) {}
friend class Bar;
private:
Foo& operator=(const Foo& other){
...
return *this;
}
};
class Bar {
public:
Foo foo;
Bar(){
foo = Foo(); // OK from here
};
};
void baz() {
Bar bar;
bar.foo.x = 42; // Ok assigning a member of foo
bar.foo = Foo(); // Invalid from here (doesn't compile)
}
Normally you would just do
struct Foo {
Bar bar;
};
Each Foo object then has a Bar subobject, which is contained within Foo itself, and whose address does not change. Assigning to bar invokes Bar's assignment operator; it doesn't change the location of bar.
If you need polymorphic behaviour from the Bar, you would do
struct Foo {
const std::unique_ptr<Bar> bar;
};
Here, since the std::unique_ptr is const, it cannot be made to point to a different Bar object after Foo's initialization, but since the Bar itself is not const, it can be modified. You could also use const std::shared_ptr<Bar>.
My best guess is that you can make the member private and then use setter member functions that achieve the mechanics that you desire (i.e. the end user can only modify your member variables in the way that you want them to be modified).
I'd like to declare a primitive type member in a class that forbids usage of operator&(). In other words: I don't want anyone to ever take the address of this member (and possibly pass it to other classes or functions, etc.)
Is this possible without using a wrapper type?
You can declare operator&() as private which prevent the address being taken with the & prefix, but std::addressof can always be used to circumvent that. Taking the address cannot be prevented, but it can be made for difficult as a deterrent.
Assume your class is A
Put this in your class declaration
A* operator&() = delete;
Declare your member as private, and your getter doesn't return reference/pointer.
it works also for non primitive-class (with the cost of the copy)
class A
{
public:
const A* operator & () const = delete; // pre-require of OP
A* operator&() = delete; // pre-require of OP.
int getMember() const { return member; }
void setMember(int value) { member = value;}
// Other stuff.
private:
int member;
};
I found this code:
foo::foo(const foo & arg) :
impl_(new impl(*arg.impl_))
{};
As far as I understand this constructor of class foo takes another object of the class foo as the only argument. What is not clear to me is why do we use * in front of arg. As far as I know, when we pass arguments by reference, we should treat this arguments in the "body" of the function as normal variables (and not as addresses of the variables, i.e. we should not use *).
The . operator has higher precedence than the indirection (*) operator, so your code is parsed as
*(arg.impl_)
impl_ appears to be a pointer, because you initialize it with new. To invoke the copy constructor, you have to pass an object, not a pointer, so you need to dereference it beforehand.
This is the copy constructor, and it takes a const reference (not an "object") as its argument.
You haven't shown the class definition, but
*arg.impl_
doesn't mean dereference arg and then look for some member called impl_, that would look like one of:
(*arg).impl_
arg->impl_
instead it means dereference the pointer arg.impl_, ie:
*(arg.impl_)
this is invoking the equivalent copy constructor for whatever type impl_ is.
Sample:
struct Impl {
int i_;
Impl() : i_(0) {}
Impl(const Impl& other) : i_(other.i_) {}
};
struct Foo {
Impl *impl_;
// Foo::Foo calls Impl::Impl
Foo() : impl_(new Impl()) {}
// Foo::Foo(const Foo&) calls Impl::Impl(const Impl&)
Foo(const Foo& other) : impl_(new Impl(*other.impl_)) {}
};
NB. this looks like the pimpl (or Pointer to Implementation) idiom.
Because impl_ is a pointer to a impl, which takes a a reference as copy constructor parameter (as is usually the case).