logo
down
shadow

How to pass `this` pointer from outer class to a member structure?


How to pass `this` pointer from outer class to a member structure?

By : haibeey
Date : October 22 2020, 06:10 PM
fixed the issue. Will look into that further C++ is not Java.
Declaring a struct inside the declaration of a class does not give the inner a pointer to the outer, there is no WIt instance associated with a ElemsCombineCycle instance, they can exist independently.
code :
void visit(const WCT& c) override {
   Node head = c.head();
   A pre = A::bottom();
   Elems pre_vector;
   pre_vector.push_back(AVC(pre));

   const WNT& cycle_n = this->_iterator.nest(head);

   this->_iterator.notify(head);

   auto comb = [this, cycle_n, head](const Elems acc, const Iterator& it) {
      Node pred = *it;
      Elems post_vector = this->_iterator.post(pred);

      if (this->_iterator.nest(pred) <= cycle_n) {

      Elems result;
      for (auto a : acc) {
        for (auto n : post_vector) {
          A pre = a.get_value();
             result.push_back(AVC(pre.join(this->_iterator.analyze(head, n.get_value()))));

          }
        }
       return result;
     }
    else
       return acc;
   };
   Elems new_pre_vector = std::accumulate(Gt::p_begin(head), Gt::p_end(head), pre_vector, comb);
  }


Share : facebook icon twitter icon
Member function of a templated class can't return a pointer to member structure?

Member function of a templated class can't return a pointer to member structure?


By : Hap
Date : March 29 2020, 07:55 AM
seems to work fine You have a few errors in your method.
1) the return type's scope must be properly qualified.
code :
template <typename T>
typename A<T>::B* A<T>::foobar(T key) // fix error 1)
{
    B* ptr = new B();
    ptr->key = key;  // fix error 2)
    return ptr;
}
Passing pointer to member func of outer class template to nested class

Passing pointer to member func of outer class template to nested class


By : Chris Mullen
Date : March 29 2020, 07:55 AM
it helps some times The problem is that you are conflating variables and template arguments. You can use constant pointers as template arguments, or you can pass variable pointers as arguments to functions.
This works:
code :
template<typename T1>
struct Outer
{
    void foo()
    {
    }

    Outer() : inner( &Outer::foo ) 
    {
    }

    template<typename T2>
    struct Inner
    {
        // Takes a pointer at runtime to any matching signature in T2
        Inner( void (T2::*f)( ) ) 
        {
        }
    };

    Inner<Outer> inner;
};

int main()
{
    Outer<int> outer;
}
template<typename T1>
struct Outer
{
    void foo()
    {
    }

    Outer() : inner( )  
    {
    }

    // Takes a pointer at compile time to a matching function in T2
    template<typename T2, void (T2::*f)()>
    struct Inner
    {
        Inner( )
        {
        }
    };

    Inner<Outer,&Outer::foo> inner;
};

int main()
{
    Outer<int> outer;
}
Can a nested class have a member pointer to the outer class?

Can a nested class have a member pointer to the outer class?


By : Anonymous
Date : March 29 2020, 07:55 AM
I hope this helps you . You can declare a pointer or reference to the outer class since it doesn't require a complete type. Declaring an object would instead require the type to be complete. See reference on incomplete types
This is the same reason why you can have a pointer to the same class as a data member:
code :
class Outer
{
  Outer* p; // Outer is an incomplete type at this point
};
How do I copy an instance of a class with a nested class containing a pointer member to the outer class?

How do I copy an instance of a class with a nested class containing a pointer member to the outer class?


By : AAbd
Date : March 29 2020, 07:55 AM
This might help you Correct, this is expected behaviour. When you create a copy of an object in C++, the compiler uses the copy constructor. If you haven't written your own copy constructor for a class, then it uses the compiler-generated copy constructor, which simply runs the (possibly generated) copy constructor for each member in turn.
So when copying an Outer, the sequence of events goes like this:
code :
Outer::Outer(const Outer& other)
    : mInnersVec(other.mInnersVec) // Do initial vector copy
{
    // Update vector elements
    for (auto& elem : mInnersVec) {
       elem.mOuterParent = this;
    }
}
How to pass pointer to a member within class to outer class?

How to pass pointer to a member within class to outer class?


By : stackofflapjacks
Date : March 29 2020, 07:55 AM
this will help You could use std::function and pass it a lambda in which you've caught this of the object you'd like to call back:
code :
#include <functional>
#include <iostream>

class Button {
public:
    void OnClick(std::function<void()> handler) {
        handler();
    }
};

class Dialog {
public:
    void handler() {
        std::cout << "Dialog::handler\n";
    }
    void render() {
        auto button = new Button;
        // a lambda catching "this" Dialog.
        button->OnClick([this] { this->handler(); });
        delete button;                               // you didn't delete your button
    }
};

int main() {
    Dialog d;
    d.render();
}
#include <iostream>

class VisualBase {
public:
    virtual void handler() = 0;
    virtual ~VisualBase() = 0;
};

VisualBase::~VisualBase() {}

class Button : public VisualBase {
public:
    void handler() override {}

    void OnClick(VisualBase* caller) {
        caller->handler(); 
    }
};

class Dialog : public VisualBase {
public:
    void handler() override { 
        std::cout << "Dialog::handler\n"; 
    }

    void render() {
        Button button;
        button.OnClick(this);
    }
};

int main() {
    Dialog d;
    d.render();
}
Related Posts Related Posts :
  • Compiler error when overriding virtual methods
  • What is the point of function pointers?
  • Which is faster in memory, ints or chars? And file-mapping or chunk reading?
  • ISO file in C++
  • Expected Class-name before { token
  • Can I trust floats or doubles representing integers to retain precision?
  • Qt execute multiple get with QNetworkAccessManager
  • Big and Little endian question
  • Visual Studio 2008 compiles anything in C++ file?
  • C++ pointer to functions, Beginner Question
  • Fastest algorithm for primality test
  • Passing array of pointers to another class
  • Handling macro redefinition without modifying .h files ... C / C++ language
  • Implement SSL with SSPI: How to start?
  • Creating a simple VS2008 visualizer inside autoexp.dat (problem with casting)
  • C++ - Unicode Newline
  • Program crashing with 'std::out_of_range' error
  • Use signed or unsigned char in constructing CString?
  • Parallel execution policies in C++ 17
  • C++ Class Templates (Queue of a class)
  • Convert image into useable byte array in C?
  • pointer to preallocated memory as an input parameter and have the function fill it
  • Animated Image in Win32
  • error on compiling statically linked library created in Qt
  • How to easily pass a very long string to a worker process under Windows?
  • Can a class add friend classes at runtime in C++?
  • C++ priority queue in ascending order by specific method for objects
  • Initialize and declare dynamically multiple variables of random entities in a loop in C++
  • Serializing, integer without libaries , getting weird results
  • Function pointers with default parameters in C++
  • Compilation errors with RapidXML
  • Bad_alloc exception when using new for a struct c++
  • count specific things within a code in c++
  • How to extract 48*48 icon image from resource of an executable file?
  • C++ Convert string (or char*) to wstring (or wchar_t*)
  • C++ include .h includes .cpp with same name as well?
  • Any way in C++ to forward declare a function prototype?
  • boost graph adjacency_list, retrieving a node's parents
  • Copy method optimization in compilers
  • what the problem in this program? it doesn't use the second number, and doesn't print the last line?
  • Need to format character precedence in Strings
  • Cross-Platform Camera API
  • sizeof continues to return 4 instead of actual size
  • String search algorithm used by string::find() c++
  • Two-phase lookup: can I avoid "code bloat"?
  • Strongest cipher available to use with C/C++?
  • Why does my program occasionally segfault when out of memory rather than throwing std::bad_alloc?
  • Firefox XULRunner or TriXul?
  • Does GetSystemInfo (on Windows) always return the number of logical processors?
  • Where is the difference between these code? Shouldn't output be the same?
  • What does it mean mean by "Expected a statement" in C++
  • keybd_event is not working right =(
  • What native C++ profiling tool do you suggest?
  • how to copy char * into a string and vice-versa
  • C++ write to front of file
  • Delphi conversion of C++ header for DVP7010B video card DLL?
  • I keep Getting Control reaches end of non-void function. What am I doing wrong?
  • How to have struct members accessible in different ways
  • Am I abusing Policies?
  • Modern equivalent of BoundsChecker for Visual Studio 2008
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk