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

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);

Related

How to do I implement oop with a number of questions [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 2 years ago.
Improve this question
I have about 40 questions. (all the answers to the questions are of either int or float data type)
I will display the question and ask the user to input his/her answer.
Then I will check if the answer is correct or not.
I need to implement this program in object oriented programming.
I thought about doing it this way,questions by questions..but is there another way of doing this? Please help me..
The code below is sort of a pseudocode . I use c++ .
class workout{
private:
float variables...;
public:
workout();
workout(float variables...);
void answers();
int Display();
~workout();
};
...
void workout::answers(){
declare variables;
display question1;
input answer1
check if correct
display question2;
input answer2
check if correct
display question3;
..and so on..
}
workout::~workout(){
}
int main(){
...
return 0;
}
Well you could have a Question class, and then go from there. Here's an example.
class Question
{
public:
std::string question
float answer;
Question(std::string _question, float _answer)
: question(_question), answer(_answer) {}
void Display() {}
bool Check(float input) {}
};
And then maybe create an array of questions?

C++ single-function variable placement [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 3 years ago.
Improve this question
I'm writing a class that reads data from a file. The project is still in development, and it's likely that I'll change the file name or path later on, so I've stored it in a std::string for quicker editing.
Given that the file name is going to be used several times in a function, but is only going to be used in one function, is there a canonical cpp rule about where I should define the variable?
//don't know where I'll define this
std::string file_name = "path/to/file.foo";
//a.h file
class A {
public:
void fileFunc();
private:
//do i define it here?
};
//a.cpp file
A::fileFunc() {
//or do i define it here?
std::ifstream in(file_name);
if(in) {
//do things
}
else {
std::cerr << "couldn't open " << file_name;
}
}
Keeps all information close to thiers use.
It will help the readability and the performance. See: https://en.wikipedia.org/wiki/Locality_of_reference
So
A::fileFunc() {
const std::string file_name = "path/to/file.foo"; // pls use const when you can
...
or
A::fileFunc(const std::string& file_name) {
...
BTW, I think this should be on https://codereview.stackexchange.com/, not stackoverflow.

How can I (cleanly!) subclass std::stringstream for a customization? [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 have a logging class with a std::stringstream member. I use it's output << overloading to get a nice easy means for catching all the data types std::stringstream gives me for free.
Here's the problem. After sending data into the stream, I want an easy way to flush it automatically to my destination (which is variable/dynamic in nature).
std::ostream will "auto flush" if you send an endl down it. That's and acceptable solution I would duplicate.
How can I implement that myself? Note that I don't want to override every operator<<() overload in std::stringstream!
I've done a similar thing. What I do is use an unnamed class instance to consume the output and put the flushing in the destructor. Something like this:
int i = 0;
MyClass() << "This is a log message containing an int: " << i;
// here, the class destructs and does whatever you need to do to flush the stream
Instead of subclassing std::stringstream, it's prefered to use composition (see Composition over inheritance).
With this, your class would look like this:
class Log{
std::stringstream _stream;
[...] // Constructor and other class logic
public:
Log& operator<<(string s){
_stream << s << endl;
return *this;
}
};

How to choose among two classes given a condition in c++? [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 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

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.