473,659 Members | 3,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_paren t(parent){}

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

private:
C(A<C>*& parent):m_paren t(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 1281
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_paren t(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_paren t(parent){}

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

private:
C(A<C>*& parent):m_paren t(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
1470
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 pre-existing legacy code that assumes the caller deletes the pointer. However, I want to reduce memory copies for the case where there are multiple clients fetching a pointer to the same memory.
1
3656
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. I know that this is quite simple if the front end is an adp/ade, but for now it's an mde, and will have to be for a while yet (it's too complex to upsize overnight).
7
5178
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 this. Ok... here goes.... I have a function that passes a pointer to a string to another function. For example: int FunctionA ()
22
1942
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, *p2; char **s1, **s2;
14
2406
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 think I need some "template". Sorry for my coding experience in c++
5
2344
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 have to "log on" with a username and password and only have access to their records. - Their will be a main table with two associated tables... - This is not the application but it is simmilar in structure... Example:
2
2161
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 the default ctor. Suppose we try to create Test obj;
2
1224
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
870
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.) A StringFormat object contains a CharacterRange array (although not as a Property). So I setup the CharacterRange array ... Dim charrange() As CharacterRange = {New CharacterRange(0, 3)} 'for first three characters (I only have one...
0
8335
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8747
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8528
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.