473,396 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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<Bab ; // Ok
A<Cac ; // 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?

Aug 28 '07 #1
2 1273
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<Bab ; // Ok
A<Cac ; // 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
Aug 28 '07 #2
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<Bab ; // Ok
A<Cac ; // 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<Tas 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<Bab;
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
Aug 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: David Coppit | last post by:
Hi all, Most smart pointers remove the need to call delete. What I want is a pointer that turns delete into a no-op for "unauthorized" clients. The reason is that I'm generating code from...
1
by: Andrew | last post by:
Hi Group! I'm looking for a tutorial in using SQL-DMO, or some sample code that will allow me to let my users switch the SQL Server database that the front end app is linking to as a back end. ...
7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
22
by: walter.preuninger | last post by:
Is there an easier way to code the cmp procedure without going thru all the pointer manipulations? #include <stdlib.h> #include <string.h> int cmp(const void *i, const void *j) { void *p1,...
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
5
by: Will | last post by:
- I know enough ASP and Access to be dangerous :) - I need to put up a data base on our web server with 3 related tables. - They will be accessed by a limited number of people. - Each user will...
2
by: subramanian100in | last post by:
If we do not provide any ctor for a class, the compiler provides the default ctor and copy ctor if needed. Consider a class Test. Suppose we provide some ctor in class Test but do not provide...
2
by: Anonymous | last post by:
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() {
0
by: eBob.com | last post by:
I stopped taking my meds two days ago and have ended up playing with text formatting methods which require a StringFormat object. (But this question really has nothing to do with text formatting.)...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.