How to choose among two classes given a condition in c++? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Suppose there are two classes Class1 and Class2. given a condition I have to choose among them in shortest way possible without using if-else.
Means least lines of code.
At compile time only!!!
class class1{};
class class2{};
auto data = (((condition) ? class1 : class2) *)(variable)

Assuming you need to create object at compile time depending on a variable, you can try something like following
class class1{};
class class2{};
int main( int argc, char *argv[] )
{
constexpr bool variable =true;
/* x is object of type class1 or class2 depending on
compile time constant 'variable'
*/
typedef std::conditional<variable, class1, class2>::type x;
//std::cout << typeid(x).name() << '\n';
return 0;
}
See Here

Related

confused about class structure in code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am reading a sample code that uses C++ and classes, I am new on C++ classes I can work with basics similar to this http://www.cplusplus.com/doc/tutorial/classes/, but I cant understand what the code below does mean or the color it is using visual studio c++
thanks
I am sorry if it is a fool question
It creates an object named some by instantiating the class some.
Then it calls the member function ToVector() on the object some and pass the result of the call to the function named function.
class is blue because it is a keyword of the C++ language.
The first some is green because it is the name of a class.
The second some is black because it is a variable.
And function and ToVector are red because the are functions.
Now this is ugly code because you "hide" the class some by reusing the same name for your variable. Also you do not need to put the word class here.
Here is a more complete and nicer version:
#include <vector>
class Some
{
public:
std::vector<int> ToVector()
{
return std::vector<int>(); //return an empty vector
}
};
int f(std::vector<int> v)
{
return 0;
}
int main(int, char**)
{
Some some; // Was "class some some"
return f(some.ToVector());
}

c++ using scope resolution operator in function call parameter [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can someone please point me to an explanation what e.g. QIODevice::WriteOnly actually does?
full line of code:
file.open(stderr, QIODevice::WriteOnly);
from that link
thanks
According to the documentation for the QIODevice class, WriteOnly is as enum constant with value 2. It indicates that the device is open for writing.
I believe that the following example for enum hack will be useful to you.
class MyClass1 {
public:
enum { SIZE=10 };
};
class MyClass2 {
public:
enum { SIZE=20 };
};
int main() {
cout << MyClass1::SIZE << "\t" << MyClass2::SIZE << endl;
}
QIODevice::WriteOnly is just a flag, you're saying that you want to open the file only for writing.
If you would want only to read the file, QIODevice::ReadOnly would be the necessary flag to use.
And to read and write use flag: QIODevice::ReadWrite:
file.open(stderr, QIODevice::ReadWrite);

expected unqualified-id before 'public' [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I've searched everywhere and cant find an answer for this error
"expected unqualified-id before 'public'"
this is my code:
#include <iostream>
using namespace std;
static int playerHP;
static int playerPWR;
public:
{
static int playerHP = 100;
static int playerPWR = 4;
}
int main(){
}
p.s. This is in my main.
I don't know why you wrote this ridiculous code, but I assume you may want this:
class Player
{
public:
static int playerHP;
static int playerPWR;
};
int Player::playerHP = 100;
int Player::playerPWR = 4;
It looks like you're trying to build a class or struct. This public statement would work inside either of those, but not alone.
Try reading this class introduction and see if that makes it clearer.
If you don't want a class, because you're not trying to build a group of similar objects, then you can place these variables inside your main() - in that case, you might want to read about variable scope.

Coding convention of using a string literal [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I know there is no specific rule of how to use or declare a string literal, like for example in my class, I want to use "MyName" string literal, and its the only class that will use it, for example,
// CFoo.h
class CFoo
{
public:
CFoo();
~CFoo();
void printString();
}
// CFoo.cpp
CFoo::CFoo()
{
}
CFoo::~CFoo()
{
}
void CFoo::printString()
{
std::cout << "MyName" << std::endl;
}
Now I want that "MyName" will have a descriptive name placeholder, like NameLiter or something like that. Should I use define preprocessor, or declare it as global in cpp as const std::string? Or should I make a private member variable and initialize it in the ctor initializer list?
Thanks!
Making it a private static const char* in CFoo would satisfy your requirements.

How to use variable type Struct inside class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have to build a videogame with SDL for my end year proyect. However I'm somewhat lost about how and when to use classes.
T tried to include a variable type struct inside a class but I can't manage to do it, maybe Position should be a class instead of a struct? Here's my code:
struct Position{
int x,y;
};
class Object{
private:
Position pos;
Position speed;
int tipe;
public:
Objeto(int,int);
Objeto();
~Objeto(); // DESTROY
};
When I try to do this I get error: 'class Object' has no member named 'x' How can I include the struct in the object?
From your error, I think you are trying to use
Objeto.x
instead of
Objeto.pos.x