reference to pointer used in ctor - using 'this' in ctor (repost- corrected sample code)

Discussion in 'C++' started by Anonymous, Aug 28, 2007.

  1. Anonymous

    Anonymous Guest

    template<class T>
    class A
    {
    public:
    A() {
    T * object = new T(this); //passing instance as parent of nested class
    m_obj.push_back(object);
    }

    ~A() {
    for (std::vector<T>::iterator it= m_obj.begin();
    it != m_obj.end(); it++) delete *it ;
    }

    private:
    std::vector<T*> m_objs ;
    };

    class B
    {
    public:
    friend class A;
    ~B(){};

    private:
    B(A<B>* parent):m_parent(parent){}

    A<B>* m_parent ;
    };


    class C
    {
    public:
    friend class A;
    ~C(){};

    private:
    C(A<C>*& parent):m_parent(parent){}

    A<C>* m_parent ;
    };


    int main( int argc, char* argv[])
    {
    A<B> ab ; // Ok
    A<C> ac ; // Croaks here ...
    };


    Class A instantiated with B compiles ok, but when instantiated with
    class C (that accepts a *&), I get the error message - cannot convert
    parameter 1 from A const* to A* - why is that ?

    Why is a reference to a ptr being passed as a const ptr ?

    If 'this' is a const ptr during construction (I doubt this assertion
    very much), then why is it ok to use as a non-const pointer in class B?
     
    Anonymous, Aug 28, 2007
    #1
    1. Advertising

  2. Re: reference to pointer used in ctor - using 'this' in ctor (repost - corrected sample code)

    Anonymous wrote:
    > [..]


    The code still doesn't compile because of numerous errors (like 'm_obj'
    and 'm_objs', declarations of friends that are templates, etc. Read
    the FAQ 5.8 please.

    > class C
    > {
    > public:
    > friend class A;
    > ~C(){};
    >
    > private:
    > C(A<C>*& parent):m_parent(parent){}


    The advice is still the same:

    C(A<C>* const& parent)...

    >
    > A<C>* m_parent ;
    > };
    >
    >
    > int main( int argc, char* argv[])
    > {
    > A<B> ab ; // Ok
    > A<C> ac ; // Croaks here ...
    > };
    >
    >
    > Class A instantiated with B compiles ok, but when instantiated with
    > class C (that accepts a *&), I get the error message - cannot convert
    > parameter 1 from A const* to A* - why is that ?
    >
    > Why is a reference to a ptr being passed as a const ptr ?
    >
    > If 'this' is a const ptr during construction (I doubt this assertion
    > very much), then why is it ok to use as a non-const pointer in class
    > B?


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 28, 2007
    #2
    1. Advertising

  3. Anonymous

    Barry Guest

    Anonymous wrote:
    > template<class T>
    > class A
    > {
    > public:
    > A() {
    > T * object = new T(this); //passing instance as parent of nested class
    > m_obj.push_back(object);
    > }
    >
    > ~A() {
    > for (std::vector<T>::iterator it= m_obj.begin();
    > it != m_obj.end(); it++) delete *it ;
    > }
    >
    > private:
    > std::vector<T*> m_objs ;
    > };
    >
    > class B
    > {
    > public:
    > friend class A;
    > ~B(){};
    >
    > private:
    > B(A<B>* parent):m_parent(parent){}
    >
    > A<B>* m_parent ;
    > };
    >
    >
    > class C
    > {
    > public:
    > friend class A;
    > ~C(){};
    >
    > private:
    > C(A<C>*& parent):m_parent(parent){}
    >
    > A<C>* m_parent ;
    > };
    >
    >
    > int main( int argc, char* argv[])
    > {
    > A<B> ab ; // Ok
    > A<C> ac ; // Croaks here ...
    > };
    >
    >
    > Class A instantiated with B compiles ok, but when instantiated with
    > class C (that accepts a *&), I get the error message - cannot convert
    > parameter 1 from A const* to A* - why is that ?
    >
    > Why is a reference to a ptr being passed as a const ptr ?
    >
    > If 'this' is a const ptr during construction (I doubt this assertion
    > very much), then why is it ok to use as a non-const pointer in class B?
    >


    I attempted to compile your code, but found the template class friend
    problem, as Victor marked else-thread.
    You inject a friend A into B, actually A is a template class, so you
    have to write A<T> as a friend of B(so B have also to be templated),

    template <class T>
    class B
    {
    public:
    friend class A<T>;
    ....

    when come to declaration of A's object,
    A<B> ab;
    B have to be changed into B<X>, where X is recursively dependent on T of
    A<T>, which is to be a templated B.
    So the recursive declaration looks like this:
    A<B<B<B....> > >

    So the job couldn't be done. Probably you have to redesign your classes.

    --
    Thanks
    Barry
     
    Barry, Aug 29, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Apricot
    Replies:
    4
    Views:
    546
    velthuijsen
    Apr 16, 2004
  2. Raf256
    Replies:
    1
    Views:
    421
    Raf256
    Jun 23, 2006
  3. NVH
    Replies:
    8
    Views:
    509
    mlimber
    Jul 6, 2006
  4. Grizlyk
    Replies:
    8
    Views:
    495
    Grizlyk
    Nov 29, 2006
  5. Anonymous
    Replies:
    2
    Views:
    384
    Victor Bazarov
    Aug 28, 2007
Loading...

Share This Page