MFC Classes CMAP - class

What I do not understand is the : “UINT&”. What does it mean in the context of the template
static CMap<CString, LPCSTR, UINT, UINT&> cXMLfields::fields_by_name;
static CMap<CString, LPCSTR, UINT, UINT&> cXMLfields::oopf_fields_by_name;
static CString friendly_name[XML_FIELDNUM];
static CString fields_by_id[XML_FIELDNUM];
static CString oopf_fields_by_id[OOPF_XML_FIELDNUM];
static void Build_map();
static void MoveItem(CListBox& src, CListBox& dst, int index);
CMap<CString,LPCTSTR, struct_sample,struct_sample> myMap;
struct_sample aTest;
aTest.a = 1;
aTest.b = 2;
aTest.c = 3;
myMap.SetAt("test",aTest);
struct_sample aLookupTest;
BOOL bExists = myMap.Lookup("test",aLookupTest); //Retrieves the
//struct_sample corresponding to "test".
I understand the theory, but my key is a String e.g “RFIDTAG1”.And my value is an unsigned integer. Example.UNIT is the Value to the Specific KEY ...for example {Age : 27} Where Age is the Key and 27 is the Value.I struggle when the value is an integer, I can understand if the value was a structure.So Can you use {Age: 27 } as the example and show me the code like below when using a structure

The & means pass by reference.
See this link.
Also here on MSDN.
ARG_VALUE
Data type used for VALUE arguments; usually a reference to VALUE.

Related

Natvis has trouble parsing a typedef'd template in VSCode

I have this (partial) code:
template<typename BaseType, unsigned int IntegerBits, unsigned int FractionalBits = std::numeric_limits<BaseType>::digits - IntegerBits>
struct fixed_point
{
private:
template <int P, typename T=void> struct power2 { static const long long value = 2 * power2<P-1,T>::value;};
template <typename P> struct power2<0, P> { static const long long value = 1; };
BaseType m_value;
const static unsigned char integer_bits = IntegerBits;
const static unsigned char fractional_bits = FractionalBits;
public:
inline fixed_point(float value) : m_value((BaseType)(value >= 0.0f ? value * power2<FractionalBits>::value + 0.5f : value * power2<FractionalBits>::value-0.5f)) { };
}
Given
typedef fixed_point<int32_t,15,16> MYFIXED_POINT ;
When I try to visualize with a natvis like this
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="fixed_point<*,*,*>">
<DisplayString>{(float)m_value / (float)(1 << $T3)}</DisplayString>
</Type>
<Type Name="Occupancy_Map::FLOAT" >
<DisplayString>{(float)m_value / (float)(1 << 16)}</DisplayString>
</Type>
</AutoVisualizer>
The top one doesn't visualize MYFIXED_POINT entirely (though it does seem to recognize this is the type of interest and will try to display things (but on the member variable lines, not the unexpanded line) and will pick up the 16 from the template (which I can tell by changing the display string to {$T3}).
It does work correctly with a fixed_point<int32_t,15,16> declared variable.
The bottom one works, but it doesn't have the template parameters, so T3 evaluates to void, which is why the hardcoded 16 is there.
I've tried fiddling with AlternativeName, but that didn't seem to help me.
Any ideas?

Accessing private member values with external template function

I'm a noob of c++, I'm trying to use a template function to get the private members inside a class because there are two types of parameters.
What I wrote is like:
template <typename Type>
Type const& Get(Type const& value)
{
return value;
}
class Event{
public:
Event(int const InputYear, int const InputMonth, int const InputDay, char const* InputContent, char const* InputNa me = "None")
:Year(InputYear), Month(InputMonth), Day(InputDay), Content(InputContent), Name(InputName)
{
}
~Event();
private:
int Year;
int Month;
int Day;
char* Content;
char* Name;
friend Type const& Get(Type const& value);
};
I don't know if my definition of friend is correct, if not could someone tell me how to use such template to access the private members?
The specification tells us that under certain circumstances (in this case explicit template instantiations), the usual access checking rules are not applied.
12 The usual access checking rules do not apply to names used to specify explicit instantiations. [ Note: In
particular, the template arguments and names used in the function declarator (including parameter types,
return types and exception specifications) may be private types or objects which would normally not be
accessible and the template may be a member template or member function which would not normally be
accessible. — end note ]
Thus, we can use this property to make a generic getter to a private member.
template <auto Event::* Member>
struct Getter {
friend auto &get(Event &e) { return e.*Member; }
};
template struct Getter<&Event::Year>;
auto &get(Event &e);
Event event{2021, 12, 24, "some event description"};
int year = get(event); // 2021

Error: passing 'const xxx' as 'this' argument discards qualifiers

I am trying to implement bigint class in c++, it's not completed yet, i have encountered some errors that i am unable understand.
I have erased all other functions (as they are unnecessary in this case)
and karatsuba is not yet completed (but that should't pose a problem in this case).
In the multiply function (overloaded * ) my compiler gives an error:
passing 'const BigInt' as 'this' argument discards qualifiers [-fpermissive]
at line
ans.a = karatsuba(n,m);
I understand that this would occur when i am trying to change a constant object or object passed to a constant function, in my case i am merely creating a new vector and passing it to karatsuba function.
Removing const from overloded * gets rid of this error.
So,does this mean that a constant function can't change anything at all? (including local variables?)
class BigInt {
typedef long long int ll;
typedef vector<int> vi;
#define p10 1000000000;
#define range 9
vi a;
bool sign;
public:
BigInt operator * (const BigInt &num) const
{
vi n(a.begin(),a.end()),m(num.a.begin(),num.a.end());
BigInt ans;
ans.sign = !(sign ^ num.sign);
while(n.size()<m.size()) n.push_back(0);
while(n.size()>m.size()) m.push_back(0);
ans.a = karatsuba(n,m);
return ans;
}
vi karatsuba(vi a,vi b)
{
int n = a.size();
if(n <= 16)
{
// some code
}
// some code
return a;
}
};
Ok so after googling a bit more, i realized that this pointer is implicitly passed to the oveloaded * and then on to karatsuba (as it is a member function of the class), and as karatsuba is not a constant function, there is no guarantee that it won't change the object contents, hence this error is triggered.
One solution is to declare karatsuba as static, as static member functions don't receive this pointer (they can even be called with out a class object simply using :: operator) , read more about them from here Static data members and member functions.
All that is needed to be changed is :-
static vi karatsuba(vi a,vi b)
{
int n = a.size();
if(n <= 16)
{
// some code
}
// some code
return a;
}

Incrementing primitive wrapper class passed as a parameter to a method which has no affect on invoker

I'm writing the Java SE 8 app on Eclipse IDE. The issue that I came across is following.
private Object[][] adjustIndexTaskValueAdded(int size){
Integer adjustingIndex = 0;
Object[][] tasksDisplay = new Object[size][taskValues[0].length];
for (int i = 0; i < size; i++) {
tasksDisplay[i][0] = taskValues[i][0];//phase colour
tasksDisplay[i][1] = identifyNextRowIDTaskTable(adjustingIndex, i);// the index
}
return tasksDisplay;
}
So, I've got adjustingIndex Integer wrapper class which I pass to the identifyNextRowIDTaskTable() method. So that the local var can store the value which gets modified at the child method.
private String identifyNextRowIDTaskTable(Integer adjustingIndex, int currentRowID){
if(UtilityOperations.isPhaseRow(phaseColourCurrent)){//it's a phase row
adjustingIndex++;
return "";
}
else{//it's a task row
int displayID = tableID - adjustingIndex;
adjustingIndex = 0;
return String.valueOf(displayID);
}
}
The above methods displays the method which modifies the Integer wrapper class which I pass to.
Now when I run the app, the new value is not reflected at the invoker method. It appears that value changes/adjusts at the child method, but the parent method does not see the changes. In the end, the outcome becomes erroneous.
The displayed source-code is simplified...
So, what the problem is?
I pass reference type var, and it is not a recursive operation.
I could use object's state to store the value instead, of-course. Yet, I want to understand the current pitfall.
Best regards
Consider
adjustingIndex++;
This is unboxing the value from the Integer to get an int and incrementing that value, this is equivalent to:
int tmp = adjustingIndex.intValue();
tmp++;
adjustingIndex = Integer.valueOf(tmp);
This resets the parameter adjustingIndex to be a reference to a new Integer, it does not change the value of the adjustingIndex variable in the calling method - that is a separate reference.
Again consider:
adjustingIndex = 0;
Again this resets the parameter adjustingIndex to be a reference to a new Integer, it does not change the value of the adjustingIndex variable in the calling method.
One alternative would be to use AtomicInteger
AtomicInteger adjustingIndex = new AtomicInteger(0);
increment with
adjustingIndex.incrementAndGet();
set back to zero with
adjustingIndex.set(0);
AtomicInteger has methods to change the value of the integer it contains, in contrast Integer is immutable and its value can't be changed.

difference between static T const and static const T

Assuming that T is an arbitrary type and we're talking about Objective-C. Then what is the difference between
static T const x = ....
and
static const T x = ....
?
They are the same.
A little background about const:
Objective-C, just like C, uses the Clockwise/Spiral Rule. In a case where you have only modifiers at the left side of the variable (of foo) you read stuff going from right to left:
int * const * var;
is read: var is a pointer to a constant pointer to integer.
However const has the same effect when it's before or after the first type:
int const var; // constant integer
const int var; // constant integer, same as above
static, instead, affects the whole variable: it's the variable to be static, not the element pointed by it or anything else: a pointer to integer can be used both to point to a static integer, or to an automatic integer without distinction, and C doesn't provide a syntax to declare a pointer able to point to static variables (or to automatic variables) only.
static int * x; // x is a static pointer to integer
int static * y; // as above y is a static pointer to integer
int * static z; // THIS SYNTAX IS NOT ALLOWED
// but if it was, it would mean the same thing
For this reason I prefer to place it before any const modifier, but it doesn't actually matter.
static const int * const * x; // this is what I do
I believe the distinction is only important for pointer types; for example 'char *'.
const char *p means that the buffer pointed to by p cannot be updated but pointer p itself can be changed (typically incremented with p++), for example:
void f(const char *p)
{
while (*p != '\0')
{
putc(*p);
p++;
}
}
char * const p means that the buffer pointed to by p can be updated but pointer p cannot be changed.
EDIT Thanks for JeremyP for the following comment:
It's quite common in Apple's APIs to define string constants (instead of using #defines), especially if the string is an NSString i.e. NSString * const kFoo = #"bar";