473,388 Members | 1,215 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,388 software developers and data experts.

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

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<Bab ; // Ok
A<Cac ; // 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?
Aug 28 '07 #1
2 1212
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.
Aug 28 '07 #2
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<Bab ; // Ok
A<Cac ; // 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
Aug 28 '07 #3

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

Similar topics

17
by: johny smith | last post by:
I never really know if I should use references or pointers for constructors. Can anyone give me some guidance on when to use what? My back ground is C, so I tend to use pointer notation, but...
4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
15
by: JKop | last post by:
Does that Standard explicitly forbid the initiation of a null reference? Is there anything wrong with the following code?: void Blah( std::string const &k ) { if ( !&k ) return; // work with...
2
by: V. de Bellabre | last post by:
Hello all, I'm currently working on a 3D graphic engine but I'm stuck with a small problem on references of pointers. Some of my c++ class are complex and use many pointers so I decided to force...
10
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in...
9
by: ziman137 | last post by:
Hi all, The results from following codes got me a bit confused. #include <stdio.h> #include <iostream> using namespace std; struct A {
1
by: Raf256 | last post by:
I have base template class A<B>, and son class B. Inside A<B> constrcutor, can I access a pointer to B, from "this"? like A<B>::A() : pointerToB(static_cast<B*>this) { } I will use the...
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.