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 been asked several times about the uses of static object and I guess I understand the crux behind using them, but one particular case confusing me, I know that in the code they are trying to make a factorial like mechanism using recursive call but did not able to understand different stages the code will go through.
#include<iostream>
using namespace std;
template<short N> class C
{
private:
static C<N-1>c;
public:
static int const n = N*c.n;
};
template<> class C<0>
{
public:
static int const n =1;
};
int main()
{
cout<<C<5>::n<<endl;
return 0;
}
See the live demo here please.
This code is generating an output 120.
What exactly is happening here?
It's unfortunate that the line
static C<N-1>c;
exists at all. It obfuscates the logic a little bit.
The functionality could have been as easily implemented as:
template<short N> class C
{
public:
static int const n = N*C<N-1>::n;
};
template<> class C<0>
{
public:
static int const n = 1;
};
which is a little bit easier to follow.
The net effect is same:
C<5>::n = 5*C<4>::n
C<4>::n = 4*C<3>::n
C<3>::n = 3*C<2>::n
C<2>::n = 2*C<1>::n
C<1>::n = 1*C<0>::n
C<0>::n = 1
Now you can follow how the program produces the output.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I implemented a new class (ProtoType) in my header file. Which looks like this:
class ProtoType : public Test
{
public:
uint32_t test();
};
class RealProtoType : public Real
{
public:
uint32_t real();
};
Then in C++ file I made this
uint32_t ProtoType::test()
{
return 5;
}
uint32_t RealProtoType::real()
{
uint32_t holder = ProtoType::test();
}
Then I get this error when compiling
error: cannot call member function ‘uint32_t ProtoType::test()’
without object uint32_t ProtoType::test();
But I still fail, how can I resolve this?
Since ProtoType::test() is a non-static member function you need an object of type ProtoType to call the function upon:
uint32_t RealProtoType::real()
{
ProtoType foo;
uint32_t holder = foo.test();
return 42;
}
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 6 years ago.
Improve this question
I am trying to humbly port "FastLED + ESP8266 Web Server" by Jason Coon: https://github.com/jasoncoon/esp8266-fastled-webserver to ESPAsyncWebServer, with a few extra goodies...
My project is located here https://github.com/kelexel/esp8266-fastled-async-webserver-2.0/
My main work on this project is to wrap everything into nice Classes, and try to afford some kind of modularity.
EDIT: As many pointed out in the comments bellow, what I try to accomplish is clearly wrongly illustrated. All I am looking for are ideas, code sample, docs, references, on what should be done, so I can learn and improve...
I am totally new to C++, so please, be indulgent..
What I am trying to accomplish is obtain what I, neophyte, can only explain as a collection of objects, each objects having a String name and method pattern. Said pattern should be a method of the ESPLedDriver.cpp Class
The issue resides in the fact that in ESPLedDriver.h and ESPLedDriver.cpp, I try to create a Struct that references ESPLedDriver::
I made a snippet of the code to illustrate the issue:
ESPLedDriver.cpp
void ESPLedDriver::colorwaves()
{
// ...
}
void ESPLedDriver::palettetest()
{
// ...
}
void ESPLedDriver::setPatterns()
{
// const uint8_t patternCount;
_patterns = {
// ERROR: src/ESPLedDriver.cpp:225:3: error: cannot convert 'ESPLedDriver::colorwaves' from type 'void (ESPLedDriver::)()' to type 'ESPLedDriver::Pattern {aka void (*)()}'
{ colorwaves, "Color Waves" },
// ERROR: src/ESPLedDriver.cpp:225:3: error: cannot convert 'ESPLedDriver::palettetest' from type 'void (ESPLedDriver::)()' to type 'ESPLedDriver::Pattern {aka void (*)()}'
{ this->palettetest, "Palette Test" },
};
}
ESPLedDriver.h
#include "FastLED.h"
class ESPLedDriver
{
public:
ESPLedDriver();
// ...
private:
void setPatterns();
typedef void (*Pattern)();
typedef Pattern PatternList[];
typedef struct {
Pattern pattern;
String name;
} PatternAndName;
typedef PatternAndName PatternAndNameList[];
/* Patterns */
void colorwaves();
void palettetest();
PatternAndNameList _patterns;
uint8_t _patternCount;
}
(Or if you prefer, as a gist:
https://gist.github.com/kelexel/ab5687cf83e376c709e49fbfbcfc100b )
If all of the objects are string and pattern, you can model them as an abstract base class:
class String_Pattern
{
public:
virtual void pattern_method(ESPLedDriver& driver_to_use) = 0;
private:
std::string text;
};
Note that this design requires passing the driver to the string pattern object. Think of this giving the object a driver to use.
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
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.
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 am starting to be more and more attracted to writing long C++ algorithmic functions using successive scoped blocks, as follows:
void my_algorithm(const MyStruct1 &iparam1, MyStruct2 &oparam2)
{
// First block
MyStruct3 intermediate_var3;
{
double temporary_var;
// Functional step 1.1
// Functional step 1.2
intermediate_var3 = ...
}
// Second block
MyStruct4 intermediate_var4;
{
double temporary_var;
// Functional step 2.1
// Functional step 2.2
intermediate_var4 = ...
}
// Final block
{
int temporary_var;
oparam2 = ...
}
}
I am starting to think it is a good way to clarify the function structure and to limit the scope of temporary variables (such as counters i, j, k etc). I saw that such scope blocks make sense in C functions to enable new declarations (see Why enclose blocks of C code in curly braces?).
In the context of C++, is this good or bad practice ?
This is a clear sign, that you should extract this separate blocks into separate functions.
MyStruct3 DoSth3(params)
{
double temporary_var;
// Functional step 1.1
// Functional step 1.2
return ...
}
MyStruct4 DoSth4(params)
{
double temporary_var;
// Functional step 2.1
// Functional step 2.2
intermediate_var4 = ...
}
void my_algorithm(const MyStruct1 &iparam1, MyStruct2 &oparam2)
{
// First block
MyStruct3 intermediate_var3 = DoSth3(params);
// Second block
MyStruct4 intermediate_var4 = DoSth4(params);
int temporary_var;
oparam2 = ...
}
It may happen, that you'll be worried about DoSth3 and DoSth4 being public, as they should be private in the context of my_algorithm. In such case you can solve it in the following way:
class my_algorithm
{
private:
static MyStruct3 DoSth3(params);
static MyStruct4 DoSth4(params);
public:
static void Perform(const MyStruct1 &iparam1, MyStruct2 &oparam2);
};