473,659 Members | 2,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

private:
B(A*& parent):m_paren t(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 1223
Correction in the sample code I sent:

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

private:
C(A*& parent):m_paren t(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_paren t(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
1633
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 really don't know what the advantage of the reference it. Thanks alot. ex. of two constructors
4
2134
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 classes
15
2217
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 k }
2
1447
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 initialisation of objects through their static function Create, which has a parameter IN-OUT that will contain the instance of the created object. Here is a sample that speaks better than words :
10
2262
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 the class template so I know how many references I have to the body. In my case it's the Integer wrapper class which is the body. This template works for many different types. The one that I use is named Integer and is a Wrapper for an int. The...
9
2464
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
1596
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 pointer.
8
2396
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 (or better a const reference). for eg, const PointRange* points = cc.points(ptAligned);
2
1281
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
8332
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,...
1
8525
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,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
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.