I have a struct:
struct s
{
UINT_PTR B_ID;
};
s d;
d.B_ID=0x1;
That works fine, but I want d.B_ID to be constant. I tried to use (const) but it didn't work. So after I put a value to d.B_ID, then I want make it a constant.
Any ideas?
EDIT
ok i don't want the whole struct a constant.
when i set timer and use the b.B_ID as an idea for the timer.
in the
switch(wparam)
{
case b.B_ID // error: B_ID must be constant
....
break;
}
so that is why i need it to be a constant
Variable modifiers are fixed at compile time for each variable. You may have to explain the context of what you are trying to do, but perhaps this will suit your needs?
struct s
{
int* const B_ID;
};
int main (void) {
int n = 5;
s d = {&n};
int* value = d.B_ID; // ok
// d.B_ID = &n; // error
return 0;
}
Since you are using C++ I would recommend:
class s {
public:
int* const B_ID;
s (int* id) :
B_ID (id) {
}
};
void main (void) {
int n = 5;
s my_s_variable = s(&n);
int* value = my_s_variable.B_ID; // ok
//my_s_variable.B_ID = &n; // error
return 0;
}
Ramiz Toma: well i need way to do it using the s.B_ID=something
In C/C++ type modifiers (like const) are declared at run time for a given type and cannot be changed at run time. This means that if a variable is declared const it can never be assigned to using the assignment operator. It will only be assigned a value when it is constructed.
This is not a problem however because you can always get around this by proper design.
If you say you need to use assignment, I assume that this is because you create the struct before you know what the value of the variable will be. If this is the case then you simply need to move the struct declaration till after you know the value.
For example
s d; //variable declaration
//calculate B_ID
//...
int* n = 5;
//...
d.B_ID = &n;
This will not work, because if you want b.D_ID to be 'un assignable' it will always be so. You will need to refactor your code similarly to:
//calculate B_ID
//...
int* n = 5;
//...
s d (&n);
//good
struct s
{
s() : B_ID(0){}
UINT_PTR const B_ID;
};
int main(){
s d;
d.B_ID=0x1; // error
}
EDIT: Sorry, here is the updated code snippet in C++
struct s
{
s(UINT_PTR const &val) : B_ID(val){}
UINT_PTR const B_ID;
};
int main(){
s d(1);
d.B_ID=0x1; // error
}
In C++ language the case label must be built from an Integral Constant Expression (ICE). ICE is what the compiler implies under the term "constant" in your error message. A non-static member of a class cannot be used in an ICE. It is not possible to do literally what you are trying to do. I.e. it is not possible to use a struct member in a case label.
Forget about switch/case in this context. Use ordinary if branching instead of switch statement.
You can't do that - ie. it is not possible to selectively make a single member of a struct const. One option is to 'constify' the entire struct:
s d;
d.B_ID=0x1;
const s cs = s; // when using this B_ID won't be modifiable - but nor would any other members
Or you could set it at construction:
struct s
{
s(UINT_PTR const p): B_ID(p) {}
UINT_PTR const B_ID;
};
s d(0xabcdef);
Another way would be a getter and a one time setter
class s
{
private:
bool m_initialized;
UINT_PTR m_value;
public:
s() : m_initialized(false), m_value(NULL) {}
s(UINT_PTR value) : m_initialized(true), m_value(value) {}
//no need for copy / assignment operators - the default works
inline UINT_PTR GetValue() const { return m_value; } //getter
bool SetValue(UINT_PTR value) //works only one time
{
if (m_initialized)
{
m_value = value;
m_initialized=true;
return true;
}
else
{
return false;
}
}
inline bool IsInitialized() const { return m_initialized; }
};
Related
A property is a public data member of a class, which can be accessed by client code. And the owning object receives a notification (in the form of get/set notification callback) whenever the client code reads or modifies the property.
Some languages (like C#) have built-in properties.
I want to create a property for C++ that will be RAM-efficient.
The most obvious way to make a property is something like this:
class Super;
struct Prop {
Prop( Super * super ) : m_super(*super), m_a(0) {}
int operator=( int a );
operator int() const;
int m_a;
Super & m_super;
};
struct Super {
Super() : one(this), two(this) {}
void onSet() { printf("set"); }
void onGet() { printf("get"); }
Prop one;
Prop two;
};
int Prop::operator=( int a ) { m_super.onSet(); m_a = a; return a; }
Prop::operator int() const { m_super.onGet(); return m_a; }
Trouble is - every property has to keep a pointer to the outer class which I consider costly.
I want to know if there is a more RAM-efficient way to do this?
For example, if all Super-classes are generated, is it allowed by the Standard to get a pointer to the outer class from this pointer of the property?
Something like this:
struct Prop {
Prop( uint8_t offset ) : m_offset(offset), m_a(0) {}
int operator=( int a );
operator int() const;
int m_a;
const uint8_t m_offset;
};
int Prop::operator=( int a ) {
Super * super = (Super *)( ((char *)this) + m_offset);
super->onSet(); m_a = a; return a;
}
struct Super {
// assuming exact order of properties
Super() : one(0), two(sizeof(Prop)) {}
void onSet() { printf("set"); }
void onGet() { printf("get"); }
Prop one;
Prop two;
};
Since this offset is a constant expression it (theoretically) can be kept in ROM (or at least it can be smaller than sizeof(pointer)).
Or maybe there is another way?
c++ has properties as language extension
Look no further, msvc has support.
clang compiler also supports this syntax. Im not sure about gcc.
Storing offset can be also be done
Just, in the constructor calculate the offset from this, ala. :
Prop( Super& super ) {
uint8_t offset = this - std::addressof(super );//somewhat unmaintable - but may save some bytes
}
then when used, calculate back using this
Please note the space saving may be less than it seems due to alignment and padding.
I obviously don't know the context of your code, so this may be inconceivable in your specific implementation, but you could do something like
class Prop(){
Prop() : m_a(0){};
int operator=(int a){m_a = a;};
int m_a;
}
class Super(){
public:
int set_prop(int index, int value){
m_props[index] = value;
onSet();
return value;
}
private:
void onSet(){};
std::vector<Prop> m_props;
}
Obviously you need to initialize the vector and handle error cases etc but the logic is there - if you only access the props through the super.
That leaves you with purely the size of the sequence of structs with no pointers back to the super.
Value of constant variable can be changed through pointer tricks, but is it possible to do something like this :
class A (){
int x;
public:
void func () const {
//change value of x here
}
}
declare x mutable
class A (){
mutable int x;
public:
void func () const {
//change value of x here
}
};
You have two options:
class C
{
public:
void const_f() const
{
x = 5; // A
auto* p_this = const_cast<C*>(this); // B
p_this->y = 5;
}
private:
mutable int x; // A
int y;
};
A: declare certain members mutable.
B: const_cast to remove constness from the this pointer.
Though this is not appreciated, but C++ provides “Backdoors” which can be used to breach its own regulations, just like dirty pointer tricks. Anyway, you can easily do this by using a casted version of “This” pointer :
class A (){
int x;
public:
void func () const {
//change value of x here
A* ptr = const_cast<A*> (this);
ptr->x= 10; //Voila ! Here you go buddy
}
}
The most important thing to understand here is bitwise/physical/concrete constness and conceptual/meaningwise/logical/abstract constness.
In short:
If the function is conceptually const, make the member data mutable.
Otherwise, make the function non-const.
Just cast 'this', this would be a dirty way to implement your program, do avoid this if you are doing a project or teamwork as others would get confused by this.
class CAST_CLASS (){
int var;
public:
void change_CAST () const {
CAST_CLASS* pointer = const_cast<CAST_CLASS*> (this);
pointer->var= 10;
}};
The other answers don't mention this, but following also modifies "x" (definitely, not advisable):
class A {
int x, &y{x}, *z{&x};
public:
void func () const
{
y = 42; // x is modified now!
*z = 29; // x is modified again!!
}
};
I'm using a library (libtcod) that has an A* pathfinding algorithm. My class inherits the callback base class, and I implement the required callback function. Here is my generic example:
class MyClass : public ITCODPathCallback
{
...
public: // The callback function
float getWalkCost(int xFrom, int yFrom, int xTo, int yTo, void *userData ) const
{
return this->doSomeMath();
};
float doSomeMath() { // non-const stuff }
};
I found a number of examples using const_cast and static_cast, but they seemed to be going the other way, making a non-const function be able to return a const function result. How can I do it in this example?
getWalkCost() is defined by my library that I cannot change, but I want to be able to do non-const things in it.
The best solution depends on why you want to do non-const stuff. For example, if you have a cache of results that you want to use to improve performance, then you can make the cache be mutable, since that preserves the logical constness:
class MyClass : public ITCODPathCallback
{
...
public: // The callback function
float getWalkCost(int xFrom, int yFrom, int xTo, int yTo, void *userData ) const
{
return this->doSomeMath();
};
float doSomeMath() const { // ok to modify cache here }
mutable std::map<int,int> cache;
};
Or perhaps you want to record some statistics about how many times the getWalkCost was called and what the maximum x value was, then passing a reference to the statistics may be best:
class MyClass : public ITCODPathCallback
{
...
public:
struct WalkStatistics {
int number_of_calls;
int max_x_value;
WalkStatistics() : number_of_calls(0), max_x_value(0) { }
};
MyClass(WalkStatistics &walk_statistics)
: walk_statistics(walk_statistics)
{
}
// The callback function
float getWalkCost(int xFrom, int yFrom, int xTo, int yTo, void *userData ) const
{
return this->doSomeMath();
};
float doSomeMath() const { // ok to modify walk_statistics members here }
WalkStatistics &walk_statistics;
};
You can hack it this way:
return const_cast<MyClass*>(this)->doSomeMath();
Of course this won't be considered good design by most people, but hey. If you prefer you can instead make doSomeMath() const, and mark the data members it modifies as mutable.
This question already has answers here:
Closed 10 years ago.
Edit Solution::
In fact, i juste forget the placment new in the copy constructor ><"
Question:
I have a weird problem. After having tried for a long momnet origin I found masi does not understand.
If someone can explain to me why.
My class:
class B; //on other file
class A {
public:
A(int type) : type(type)
{
switch(type)
{
case TOKEN:
{
for(int i=0;i<4;++i)
new(&token.h[i].link) shared_ptr<B>; //< init the ptr on the addr (because of union)
}break;
case OTHER: {}break;
}
}
~A()
{
switch(type)
{
case TOKEN:
{
for(int i=0;i<4;++i)
{
/*option 1*/ token.h[i].link.~shared_pt<B>(); //< Make seg fault
/*option 2*/ token.h[i].link.reset(); //< ok
}
}break;
case OTHER: {}break;
}
}
}
enum {TOKEN=0,OTHER} type;
union {
struct {
double score;
struct {
std::shared_ptr<B> link;
double to_find;
} h [4];
}token;
struct {
//else
} other;
}
};
My code:
void f()
{
vector<A> vec;
A tmp = A(A::TOKEN);
vec.emplace_back(tmp);
}
Option 1: this causes an error when leaving f;
option 2: Ok but ~shared_ptr() is not call, so it make memory leak, right?
If you have an idea that could help me understand who is wrong.
Edit:
I use C++11 with gcc.4.6.3 on Ubuntu 12.04x86.
Original code:
class stack_token {
public:
stack_token();
stack_token(const stack_token& other);
stack_token(const int i,Parser::peptide::peak* data); //peak
stack_token(const int i,const double e,AnalyseurPeptide::stack_token* peak); //aa
stack_token(const int i); //aa pour boucher un trou
stack_token(const double score); //HEADER
~stack_token();
stack_token& operator=(const stack_token& other);
inline stack_token* get_peak_stack_NULL() {
stack_token* res = aa_token.pt_data;
aa_token.pt_data=NULL;
return res;
};
void __print__() const;
enum Type {UNKNOW=-1,AA_TOKEN=0,AA_HOLD_TOKEN,/*AA_LIST,*/PEAK_TOKEN, HEADER_TOKEN} type;
union {
struct {
int index;
double error;
stack_token* pt_data;
} aa_token;
struct{
double error;
stack_token* pt_data;
std::vector<int> aa_index;
} aa_hold_token;
struct {
int index;
Parser::peptide::peak* pt_data;
} peak_token;
struct {
double score;
struct {
std::shared_ptr<std::list<list_arg> > link;
double to_find;
} holds [Parser::peptide::SIZE];
} header_token;
};
};
stack_token::~stack_token()
{
switch(type)
{
case AA_TOKEN:
{
if(aa_token.pt_data != NULL)
delete aa_token.pt_data;
}break;
case AA_HOLD_TOKEN :
{
aa_hold_token.aa_index.~vector<int>();
}break;
case PEAK_TOKEN :
{
}break;
case HEADER_TOKEN :
{
for (int i=0;i<Parser::peptide::SIZE;++i)
header_token.holds[i].link.reset();//~shared_ptr<std::list<list_arg> >();
}break;
default : break;
}
};
stack_token::stack_token()
{
this->type = UNKNOW;
};
stack_token::stack_token(const int i,Parser::peptide::peak* data) //peak
{
this->type=PEAK_TOKEN;
peak_token.index = i;
peak_token.pt_data = data;
};
stack_token::stack_token(const int i,const double e,AnalyseurPeptide::stack_token* peak) //aa
{
this->type=AA_TOKEN;
aa_token.error =e;
aa_token.index = i;
aa_token.pt_data = peak;
};
stack_token::stack_token(const int i)
{
this->type=AA_HOLD_TOKEN;
aa_hold_token.error = 0;
aa_hold_token.pt_data = this;
new(&aa_hold_token.aa_index) vector<int>();
};
stack_token::stack_token(const double score) //HEADER
{
this->type = HEADER_TOKEN;
header_token.score = score;
for (int i=0;i<Parser::peptide::SIZE;++i)
new (&header_token.holds[i].link) shared_ptr<list<list_arg> >;
#warning "add to_find init"
};
Code that fail:
void save_stack(const std::list<stack_token*>& search, std::list<std::vector<stack_token> >& res)
{
vector<AnalyseurPeptide::stack_token> l;
auto i=search.begin();
auto end = search.end();
stack_token tmp = stack_token(0.f); /* if I remove this */
l.emplace_back(tmp); /* and this, all is ok */
while(i!=end)
{
l.emplace_back(**i); //< fail here
++i;
}
res.emplace_back(l);
}
If you're compiling with C++03, the code is illegal, because
C++03 doesn't allow types with non-trivial default constructors,
copy constructors, assignment operators or destructors in
a union. With C++11, the code is illegal, because if the union
contains any of the above, the compiler deletes the
corresponding member of the union. So your union has no default
constructor, copy constructor, assignment or destructor. Which
means you can't instantiate it, or use it in any way. And which
means that the default constructor needed by A::A(int) doesn't
exist, and that the compile should complain when you define this
function (or any constructor of A).
If the compiler compiles such code, it means that the compiler
doesn't implement the new union stuff correctly, and thus, that
you cannot use it.
With regards to what actually happens: I suspect that the
compiler is using bitwise copy in the copy constructor of A
(rather than refusing to generate it). vec.emplace_back(tmp)
uses the copy constructor to create the new element in vec.
Bitwise copy means that you end up with two instances of
a shared_ptr which point to the same object, but which both
have a count of 1. The first one destructs correctly, and the
second accesses deleted memory. Boom.
The simplest way to solve your problem is to use
boost::variant (which means defining the struct in the union
somewhere outside of the union, and giving them a name). If for
some reason you cannot use Boost, it's relatively trivial to
implement by hand, along the lines of what you are doing. In
the union itself, you just have unsigned char token[
sizeof(TokenType) ]; etc., for each non-POD member, with some
additional members if necessary to ensure alignment (on most
processors, a double will do the trick). You then use
reinterpret_cast on the name of the array to get a pointer to
the desired type, placement new to initialize it, and explicit
destruction to destruct it, much along the lines you've done.
And you implement a copy constructor and an assignment
operator that work, and take into account the types as well.
(It's not that difficult. I've done it one or two times: for
tokens in a parser, for modeling tables which we get from Excel,
etc.)
Technical problems:
union (don't),
uninitialized,
rule of three (not taking properly charge of copying)
Design problems:
Representing types as numbers. Represent types as types.
Keep the knowledge you gained from writing that code, and start from scratch again.
Very little more can be meaningfully said until you post the real code (e.g. swithc will never compile: what you posted is not the real code).
I have a constant value that never changes during run-time, but is impossible to know until run-time.
Is there a way to declare a constant (either as a member of a class or not) without defining it and also assign a computed value once (and only once) it is determined; or am I going to have to resort to a non-const declaration and use coding S & Ps (ALL_CAPS variables names, static declaration if in a class, etc.) to try and keep it from changing?
CLARIFICATION:
Though these are good answers, the real-world situation I have is more complicated:
The program has a main loop that continually runs between processing and rendering; the user can set required options and once they are set they will never change until the program is restart. An "Initialize" function is set up for anything that can be determined before the main loop, but values that are dependent on user interaction must be performed in the middle of the loop during the processing phase. (At the moment, persistent data storage techniques come to mind...)
Something like this?
const int x = calcConstant();
If it's a class member, then use the constructor initialisation list, as in Yuushi's answer.
You can define it in a struct or class and utilize an initialisation list:
#include <iostream>
struct has_const_member
{
const int x;
has_const_member(int x_)
: x(x_)
{ }
};
int main()
{
int foo = 0;
std::cin >> foo;
has_const_member h(foo);
std::cout << h.x << "\n";
return 0;
}
As a static or function-local variable:
const int x = calcConstant();
As a class member:
struct ConstContainer {
ConstContainer(int x) : x(x) {}
const int x;
};
Yes, you can make a private static singleton field with an initialization method and a gettor method. Here's an example of how to do it:
// In foo.h
class Foo
{
public:
// Caller must ensure that initializeGlobalValue
// was already called.
static int getGlobalValue() {
if (!initialized) {
... handle the error ...
}
return global_value;
}
static void initializeGlobalValue(...)
private:
static bool initialized;
static int global_value;
};
// In foo.cpp
bool Foo::initialized = false;
int Foo::global_value;
void Foo::initializeGlobalValue(...) {
if (initialized) {
...handle the error...
}
global_value = ...;
initialized = true;
}