dimanche 16 janvier 2022

Factory Object for different objects types

Suppose to have the following class structure:

template <typename T, typename U>
class Base
{
  public:
    virtual ~Base<T, U>(){};
    virtual U show() = 0;

  protected:
    T foo;
};

class DerivateA : public Base<int, int>
{
  public:
    DerivateA() { foo = 7; };
    virtual int show() override { return foo; };
};

class DerivateB : public Base<std::string, std::string>
{
  public:
    DerivateB() { foo = "Hello"; };
    virtual std::string show() override { return foo; };
};

Is there any way I can create a factory object with a Create() function that, based on an input value, returns a pointer to either DerivateA or DerivateB?

samedi 15 janvier 2022

Why can't I use an arbitrary nesting of braces to construct most classes?

Given the following code:

struct A;

struct B {
    B() {}
    B(A &&) {}
};

struct A {
    A() {}
    A(B &&) {}
};

Then I can use as many braces as I want to construct A or B.

// default construct A
auto a = A{};
// default construct B, forward to A
auto b = A;
// default construct A, forward to B, forward to A
auto c = A};
// etc.
auto d = A}};
auto e = A}}};

Similarly, given

struct C {
    C(std::initializer_list<C>) {}
};

then I can also use as many braces as I want

// default construct C
auto f = C{};
// construct from initializer_list of one default constructed C
auto g = C;
// construct from initializer_list of one C constructed from empty initializer_list
auto h = C};
// etc.
auto i = C}};
auto j = C}}};

Why doesn't the same argument work for a truly boring type?

struct D {
};

or, rewritten for clarity:

struct D {
    D() {}
    D(D &&) {}
};

This fails even on

auto k = D;

Why does this not default construct a D with the innermost braces, and then pass that rvalue on to the move constructor of D?

See it live: https://godbolt.org/z/E763EPGh1

C++ SFML src/Utility/FileSystem.hpp:8:36: fatal error

I tried to install SFML with the help of this tutorial because I couldn't find a way to install it myself. After I did CTRL + B and Run build & debug I got this error:

⬤  Build & Run: Debug (target: sfml-vscode-boilerplate.exe)

   src/PCH.hpp
In file included from src/PCH.hpp:66:0:
src/Utility/FileSystem.hpp:8:36: fatal error: experimental/filesystem: No such file or directory
  #include <experimental/filesystem>
                                    ^
compilation terminated.
Makefile:313: recipe for target 'bin/Debug/obj/PCH.hpp.gch' failed
mingw32-make[1]: *** [bin/Debug/obj/PCH.hpp.gch] Error 1
Makefile:262: recipe for target 'all' failed
mingw32-make: *** [all] Error 2

✘  Failed!
   Review the compile errors above.

how to create a c++ recursive function to return a node custom object?

how are you?

I created a C++ recursive function in order to iterate over a binary tree and print out all the NODEs where the property COMPLETED = TRUE;

It´s working pretty fine because the type of the function is VOID and I am only printing out the result.

This is the way that works fine:

void findAndPrintFirstCompletedNodes(treeNode *lastNode) {
     if (lastNode == 0){
         return;
     }
     
   if (lastNode->completed == true) {
          cout << lastNode->word.morseWordElement << endl;
   
   }
      findAndPrintFirstCompletedNodes(lastNode->left);
      findAndPrintFirstCompletedNodes(lastNode->right);
   
 }

But what I want to do is to return the first found "COMPLETED" node instead of just printing!

I tried this way but is not working:

treeNode * findAndPrintFirstCompletedNodes(treeNode *lastNode) {
      if (lastNode == 0){
          return 0;
      }
      
    if (lastNode->completed == true) {
           return lastNode;
    
    }
       findAndPrintFirstCompletedNodes(lastNode->left);
       findAndPrintFirstCompletedNodes(lastNode->right);
    
  }

Thanks for the help.

Filipe

Getting Extra characters at the end when Creating std::string from char*

I have just started learning C++. Now i am learning about arrays. So i am trying out different examples. One such example is given below:

int main()
{
    const char *ptr1 = "Anya";
    char arr[] = {'A','n','y','a'};
    
    std::string name1(ptr1); //this works 
    std::cout << name1 << std::endl;
    
    std::string name2(arr); 
    std::cout << name2 << std::endl; //this prints extra characters at the end?
    return 0;
}

In the above example at the last cout statement i am getting some extra characters at the end. My question is that how can i prevent this from happening in the above code and what is wrong with the code so that i don't make the same mistake in future?

After calling the reset method on a unique pointer object, I am able to use the object without getting an access violation error

As you below sample code, I am trying to use a unique pointer not give an error

int main()
{
    auto a = make_unique<TestClass>();
    a->Print();
    a.reset();
    a->Print();// this method call is still working
}

How to return different types according to constexpr parameter in C++ 11?

Example code:

class A {
// ......
}

class B {
// ......
}


// type of var1 is A
constexpr auto var1 = function_or_macro("A");


// type of var2 is B
constexpr auto var2 = function_or_macro("B");

// compile error
// constexpr auto var3 = function_or_macro("Other");

Can I write a template function or macro function_or_macro() to achive this goal in C++11?

The input parameter is always constexpr, I think the compiler can determain the return value type at compile time in this case. But I can't found a way to write the correct code.

Note: Can not use function_or_macro<A>("A").