C++: Conditional changes in a header file - c++

I am working on a feature, the code changes of which need to be guarded by checking registry keys. In .cpp files, I can simply check if the registry key is on or not and subsequently add the code changes. Here's an example:
bool isRegistryKeyOn = SomeMethodToCheckRegistryKey();
if(isRegistryKeyOn)
{
// New code goes here..
}
else
{
// Older one's here..
}
Now, I am facing a problem during changes in the header file. Suppose the header contains the following declarations:
class SomeClass
{
long var;
...
};
typedef int A;
And as part of the feature, I need this:
class SomeClass
{
long long var;
...
};
typedef int A;
typedef long long B;
How can I get around this requirement? All solutions, including the out-of-the-box ones are welcome!

Are you hoping to avoid changing the type in multiple places?
You're already using typedefs, why don't you nest them:
typedef long long T;
class SomeClass
{
T var;
//...
};
typedef int A;
typedef T B;

Related

If statement inside Struct Declaration C++

I have a doubt regarding structs and if statements in C++
For the sake of simplicity, I have created a sample code to explain my intention
int var = 10
struct example{
int a;
int b;
if(var > 8){
int c;
}
};
I have a codebase which uses similar kind of code as above. Commenting out the if portion does not give any errors.
My question is
Could if statements be put in struct declarations?
If not, what is the possible remedy for this, since if statment is mandatory.
Note: I cannot use #if,#else directives nor std::optional or other standard libraries to mitigate this, so please help me find another solution.
No you can't use if statement inside your struct or class definition. Instead, for condition declaration, you can use #if directive.
#define var 10
struct example {
int a;
int b;
#if var > 8
int c;
#endif
}
This will work.
'#if' is a compiler directive (pre-processor directive). if statement on the other hand is runtime statement.
Other than this there is no other way!
int main()
{
int var = 10;
if(var>10){
struct example{
int a;
int b;
int c;
};
}else {
struct example{
int a;
int b;
};
}
cout<<"Hello World";
return 0;
}
There are multiple ways to address your problem one way of achieving is shown.
We cannot add if Statement in the Structure.

Use enum to determine type of return result ( a hack using Macro )

I have many types of game-object that are related together is some ways.
All relations is implemented by Map<K1,K2>.
#include <vector>
using namespace std;
template<class K1,class K2> class Map{ //N:N relation
public: std::vector<K2*> getK2(K1* k1){/* some code */return std::vector<K2*>();}
public: std::vector<K1*> getK1(K2* k2){/* some code */return std::vector<K1*>();}
//... various function ...
};
Here is the hub class GameRelation that facilitates all relation query :-
(just an example, no need to pay attention to all detail)
class Human{}; class House{}; class Dog{};
class GameRelation{
public:
#define RELATION(A,B,EnumName) Map<A,B> Map##EnumName; \
enum EnumName##Enum{EnumName}; \
std::vector<B*> getAllRight(EnumName##Enum e,A* a){ \
return Map##EnumName.getK2(a); \
}
//... various function ...
RELATION(Human,House,Own)
//I can insert any relation that I want
};
The above macro expands into something like :-
Map<Human,House> MapOwn;
enum OwnEnum{Own};
std::vector<House*> getAllRight(OwnEnum e,Human* a){
return MapOwn.getK2(a);
}
Here is how it can be used (full demo):-
int main() {
GameRelation gameRelation;
std::vector<House*> houses=gameRelation.getAllRight(GameRelation::Own,new Human());
//get all "House" that is "Own" by a "Human"
return 0;
}
After some testing, it works good. Everyone is happy with the magical result.
However, my conscious tell me that it is a hack.
It is also a little bad for content-assist (e.g. intellisense) and automatic refactoring.
I also need the awesome hacking X-MACRO if I want to move their implementation to .cpp.
Question:
Is there any elegant (less hack) way? What is it?
"No" can be a valid answer.
Is X-MACRO the (professional) way to go when I need such (strange) feature?
struct GameRelation{
template <typename A, typename B>
struct Relation {
std::vector<B*> getAllRight(A* a) {
return map.getK2(a);
}
private:
Map<A, B> map;
};
Relation<Human, House> own;
};
int main() {
GameRelation gameRelation;
std::vector<House*> houses = gameRelation.own.getAllRight(new Human());
}

Is there another reason for using tags in typedef except for self referencing?

If I want to create a new struct type that contains a pointer the type itself I wouldn't be able to do
typedef struct {
my_type* p;
} my_type;
and I will need to use a tag
typedef struct my_tag {
struct my_tag* p;
} my_type;
Is there another use for the tag mechanism? because I've seen it several times and I don't understand why someone would add this to a typedef
Also, since C and C++ are a bit different with their struct keyword, would that change anything if I typedef such a struct in C or in C++?
Another use is "opaque type", which is usually forward- declared as
typedef struct my_type my_type;
where you want to define a type at the same time as you do a forward declaration. typedef struct my_type; would have been nonsense. In general, you can't forward-declare without a struct tag.
I looked over the comments and answer, but never saw this. It seems too obvious for someone to not have already brought it up, so I apologize if this duplicates a response.
In C++, you don't use typedef struct. Instead, add a forward declaration of the incomplete type to the member's declaration:
#include <iostream>
struct my_type {
int i;
struct my_type* p;
};
int main() {
my_type my_obj;
my_obj.i = 100;
my_obj.p = &my_obj;
std::cout << my_obj.p->i << '\n';
}

xcode 7.3: how to put a constant in a struct

I want to put constants in a struct, but compiler generates error that ";" is missing at the "=".
struct {
int aaa=111; // error: "expected ; at end of declaration list"
} blah;
Did you try maybe:
int aaa{111};
And if you need int as a constant you should probably include the const keyword.
const int aaa{111};
You can't initialize at the time of defining the structure in Obj-C. Initialization is possible at the time of creating instance shown as below.
struct Employee {
int idNumber;
int age;
};
// create instance
struct Employee emp1;
emp1.idNumber=12345;
emp1.age = 25;
If you're using Objective-C, in an .h file add something like:
extern const struct MyStruct {
int aaa;
} MyStruct;
In the .m file:
const struct MyStruct MyStruct = {
.aaa = 1
};
#import the .h file and use the struct in your code like this:
if (someInteger == MyStruct.aaa) ...

Import c enum to c++ class definition

I have a class like this:
class TType {
public:
...
enum binary_type {
bt_a = 0,
bt_xyz,
....
bt_ak = 10,
....
};
}
and I use it in several places, also the enum:
if(var12 == TType::bt_a ) { ....
Now I imported a C library which has exactly the same enum (same keys, same values, same size) inside one of it's headerfiles:
typedef enum data_types_e {
bt_a = 0,
bt_xyz,
....
} data_types;
How can I define the enum in the c++ class definition to use the declaration of the c headerfile?
I want to continue using the enum the same way as before (TType::bt_a), and avoid copying the whole enum. Furthermore I don't wont to modify the library (otherwise a preprocessor-macro would do the trick) and I want changes made in the library also be made to the enum in my class.
Neither a typedef inside the c++ class definition nor a type alias (c++11) seem to work in this situation.
"How can I define the enum in the c++ class definition to use the declaration of the c headerfile?"
You can simply reuse the values from the c-style enum:
#include "TheOtherEnum.h"
...
enum binary_type {
bt_a = ::bt_a,
bt_xyz = ::bt_xyz,
....
bt_ak = ::bt_ak,
....
};
"Neither a typedef inside the c++ class definition nor a type alias (c++11) seem to work in this situation."
Yes these would work to provide the correct enum type, but you'll still need to qualify the values from the global namespace and not for nested to your class.
You can make C++ enum dependant of C enum:
typedef enum data_types_e {
bt_a = 0,
bt_xyz,
....
} data_types;
// ...
class TType {
public:
...
enum binary_type {
bt_a = bt_a,
bt_xyz = bt_xyz,
....
};
}
If possible, try renaming your class TType say, class TType_1.
//=============CLibraryFileContainingEnum.h=================
typedef enum data_types_e
{
bt_a = 9999,
bt_xyz
} data_types;
//==========================================================
//==========================================================
class TType_1
{
public:
enum binary_type
{
bt_a = 8878,
bt_xyz
};
};
namespace TType
{
#include "CLibraryFileContainingEnum.h"
}
int main()
{
int a = TType::bt_a; //this prints 9999
cout << a << endl;
return 0;
}
//==========================================================
Here is my answer, this is an old topic, but better lately than never
class TType {
public :
public:
...
#undef __cplusplus
#include "yourheaderc.h"
#define __cplusplus
}
maybe with a typedef binary_type data_types to preserve your nominations
But this solution is available if your header don't contains syntax C forbidden in C++. I'm currently searching a new solution because there are prototypes in my header that contains something like :
void afunction( unsigned long id,
enum T_TYPE type,
enum T_CHAR characteristic,
unsigned short number,
unsigned short value);
because T_TYPE and T_CHAR are not typedef-ed but this syntax is non-sense in C++ because it's the declaration syntax. So my solution is not appropriate if you are in a similar case.