Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reference to pointer used in ctor (using 'this' in ctor)

Reply
Thread Tools

reference to pointer used in ctor (using 'this' in ctor)

 
 
Anonymous
Guest
Posts: n/a
 
      08-28-2007
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* parent):m_parent(parent){}

A* m_parent ;
};


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

private:
B(A*& parent):m_parent(parent){}

A* m_parent ;
};


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


Class A instanstiated 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?
 
Reply With Quote
 
 
 
 
Anonymous
Guest
Posts: n/a
 
      08-28-2007
Correction in the sample code I sent:

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

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

A* m_parent ;
};

BTW, that typo was (OBVIOUSLY), NOT the reason for the error message I
reported getting earlier.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-28-2007
Anonymous wrote:
> [..]
> class C
> {
> public:
> friend class A;
> ~C(){};
>
> private:
> B(A*& parent):m_parent(parent){}


I am sure you meant

C(A* const& parent) : m_parent(parent) {}

>
> A* m_parent ;
> };
>
>
> int main( int argc, char* argv[])
> {
> A<B> ab ; // Ok
> A<C> ac ; // Croaks here ...
> };
>
>
> Class A instanstiated 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?


'this' cannot be changed _itself_, that's why you need to put 'const'
right before the '&' in the declaration of the argument.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reference to pointer used in ctor - using 'this' in ctor (repost- corrected sample code) Anonymous C++ 2 08-29-2007 06:26 AM
copy ctor vs default ctor subramanian100in@yahoo.com, India C++ 2 08-15-2007 10:49 AM
ctor/dtor calls and ctor init seq Grizlyk C++ 8 11-29-2006 06:35 AM
Templates, copy ctor and type-conversion ctor NVH C++ 8 07-06-2006 07:19 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM



Advertisments