473,769 Members | 6,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing ref to ptr again as ref to ptr....

Let's say I have this code

--------------------
class GenericQueue
{
public:
bool Pop(void*& refToPtr); // //--------------------(1)

};

class SpecialQueue: private GenericQueue
{
public:
bool Pop(T*& refToPtr)
{
return
GenericQueue::P op(refToPtr); //--------------------(2)
}

};
Why doesn't the statement in -------(2) compile?
Jun 27 '08 #1
23 2152
On Apr 24, 1:37 pm, osama...@gmail. com wrote:
Let's say I have this code

--------------------
class GenericQueue
{
public:
bool Pop(void*& refToPtr); // //--------------------(1)

};

class SpecialQueue: private GenericQueue
{
public:
bool Pop(T*& refToPtr)
{
return
GenericQueue::P op(refToPtr); //--------------------(2)
}

};

Why doesn't the statement in -------(2) compile?

And I did have the

template<class Tin front of SpecialClass. Sorry!. I just posted a
snippet here.
Jun 27 '08 #2
os******@gmail. com wrote:
On Apr 24, 1:37 pm, osama...@gmail. com wrote:
>Let's say I have this code

--------------------
class GenericQueue
{
public:
bool Pop(void*& refToPtr); // //--------------------(1)

};

class SpecialQueue: private GenericQueue
{
public:
bool Pop(T*& refToPtr)
{
return
GenericQueue:: Pop(refToPtr); //--------------------(2)
}

};

Why doesn't the statement in -------(2) compile?
Because T*& and void*& are different types with no automatic conversions
between them.
And I did have the

template<class Tin front of SpecialClass. Sorry!. I just posted a
snippet here.
Why do you run your own code for queue? It seems as though std::queue<T*>
might be what you are looking for.
Best

Kai-Uwe Bux
Jun 27 '08 #3
On Apr 24, 7:14 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
osama...@gmail. com wrote:
On Apr 24, 1:37 pm, osama...@gmail. com wrote:
Let's say I have this code
--------------------
class GenericQueue
{
public:
bool Pop(void*& refToPtr); // //--------------------(1)
};
class SpecialQueue: private GenericQueue
{
public:
bool Pop(T*& refToPtr)
{
return
GenericQueue::P op(refToPtr); //--------------------(2)
}
};
Why doesn't the statement in -------(2) compile?

Because T*& and void*& are different types with no automatic conversions
between them.
And I did have the
template<class Tin front of SpecialClass. Sorry!. I just posted a
snippet here.

Why do you run your own code for queue? It seems as though std::queue<T*>
might be what you are looking for.

Best

Kai-Uwe Bux
I am implementing a lockless queue, which I don't think the
std::queue<T*pr ovides. To avoid template-induced code bloat, I am
implementing a generic the queue using void* pointers. But to gain
type-safety, I am implementing a templated interface-class.

Now what is confusing me is that this function works
SpecialQueue::P ush(T* ptr) { GenericQueue::P ush(ptr); }

and it converts T* to void* no problem.

But when I do

SpecialQueue::P op(T*& refPtr) { GenericQueue::P op(refPtr); }

Now let's say I instantiate an object

SpecialQueue<in tspQ();
int a = 10;
int p = &a;
spQ.Pop(p);

It gives me this error:

"cannot convert parameter 1 from 'int *' to 'void *&'"
Jun 27 '08 #4
On Apr 24, 7:14 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
osama...@gmail. com wrote:
On Apr 24, 1:37 pm, osama...@gmail. com wrote:
Let's say I have this code
--------------------
class GenericQueue
{
public:
bool Pop(void*& refToPtr); // //--------------------(1)
};
class SpecialQueue: private GenericQueue
{
public:
bool Pop(T*& refToPtr)
{
return
GenericQueue::P op(refToPtr); //--------------------(2)
}
};
Why doesn't the statement in -------(2) compile?

Because T*& and void*& are different types with no automatic conversions
between them.
Sorry. Why don't we need automatic type conversion from T* to void*
but need it from T*& to void*&.
I am still learning C++, so please excuse my ignorance.
Jun 27 '08 #5
On 2008-04-25 12:32:38 -0400, os******@gmail. com said:
>
Sorry. Why don't we need automatic type conversion from T* to void*
but need it from T*& to void*&.
I am still learning C++, so please excuse my ignorance.
You need the type conversion in both cases. The difference is that the
first conversion is part of the language, and the second one is not.

void f0(void*);
void g(int *pi)
{
f0(pi); // okay: automatic conversion from int* to void*
}

void f1(void*&);
void h(int *&rpi)
{
f1(rip); // error: no conversion from int*& to void*&
}

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #6
os******@gmail. com wrote:
On Apr 24, 7:14 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
>osama...@gmail .com wrote:
>>On Apr 24, 1:37 pm, osama...@gmail. com wrote:
Let's say I have this code
>>>--------------------
class GenericQueue
{
public:
bool Pop(void*& refToPtr); // //--------------------(1)
>>>};
>>>class SpecialQueue: private GenericQueue
{
public:
bool Pop(T*& refToPtr)
{
return
GenericQueue ::Pop(refToPtr) ; //--------------------(2)
}
>>>};
>>>Why doesn't the statement in -------(2) compile?

Because T*& and void*& are different types with no automatic
conversions between them.
>>And I did have the
>>template<clas s Tin front of SpecialClass. Sorry!. I just posted
a snippet here.

Why do you run your own code for queue? It seems as though
std::queue<T*m ight be what you are looking for.

Best

Kai-Uwe Bux

I am implementing a lockless queue, which I don't think the
std::queue<T*pr ovides. To avoid template-induced code bloat, I am
implementing a generic the queue using void* pointers.
And you believe you can outsmart the compiler?

I wouldn't bet on that. ;-)
Bo Persson
Jun 27 '08 #7
On Apr 25, 12:48 pm, "Bo Persson" <b...@gmb.dkwro te:
osama...@gmail. com wrote:
On Apr 24, 7:14 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
osama...@gmail. com wrote:
On Apr 24, 1:37 pm, osama...@gmail. com wrote:
Let's say I have this code
>>--------------------
class GenericQueue
{
public:
bool Pop(void*& refToPtr); // //--------------------(1)
>>};
>>class SpecialQueue: private GenericQueue
{
public:
bool Pop(T*& refToPtr)
{
return
GenericQueue: :Pop(refToPtr); //--------------------(2)
}
>>};
>>Why doesn't the statement in -------(2) compile?
Because T*& and void*& are different types with no automatic
conversions between them.
>And I did have the
>template<cla ss Tin front of SpecialClass. Sorry!. I just posted
a snippet here.
Why do you run your own code for queue? It seems as though
std::queue<T*mi ght be what you are looking for.
Best
Kai-Uwe Bux
I am implementing a lockless queue, which I don't think the
std::queue<T*pr ovides. To avoid template-induced code bloat, I am
implementing a generic the queue using void* pointers.

And you believe you can outsmart the compiler?

I wouldn't bet on that. ;-)

Bo Persson
Why not?

If you instantiate GenericQueue a dozen times, you'll have a dozen
copies of its code. But if you use GenericQueue for storing void*
pointers instead, make its ctor, copy ctor, and assig operator
private, you'll prevent clients from making instances of it AND you'll
have only one version of the code. And the templated interface class
will implement its functionality in-terms-of GenericQueue
functionality and provide for type safety. The code in the interface
class is substantially less than that of the GenericQueue and will not
result in as much code bloat. No?

Of course I'll have to account for the fact that a pointer to an
object might have been pushed onto multiple stacks.

Jun 27 '08 #8
On Apr 25, 1:09 pm, osama...@gmail. com wrote:
On Apr 25, 12:48 pm, "Bo Persson" <b...@gmb.dkwro te:
osama...@gmail. com wrote:
On Apr 24, 7:14 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
>osama...@gmail .com wrote:
>>On Apr 24, 1:37 pm, osama...@gmail. com wrote:
>>>Let's say I have this code
>>>--------------------
>>>class GenericQueue
>>>{
>>>public:
>>> bool Pop(void*& refToPtr); // //--------------------(1)
>>>};
>>>class SpecialQueue: private GenericQueue
>>>{
>>>public:
>>> bool Pop(T*& refToPtr)
>>> {
>>> return
>>>GenericQueue ::Pop(refToPtr) ; //--------------------(2)
>>> }
>>>};
>>>Why doesn't the statement in -------(2) compile?
>Because T*& and void*& are different types with no automatic
>conversions between them.
>>And I did have the
>>template<clas s Tin front of SpecialClass. Sorry!. I just posted
>>a snippet here.
>Why do you run your own code for queue? It seems as though
>std::queue<T*m ight be what you are looking for.
>Best
>Kai-Uwe Bux
I am implementing a lockless queue, which I don't think the
std::queue<T*pr ovides. To avoid template-induced code bloat, I am
implementing a generic the queue using void* pointers.
And you believe you can outsmart the compiler?
I wouldn't bet on that. ;-)
Bo Persson

Why not?

If you instantiate GenericQueue a dozen times, you'll have a dozen
copies of its code. But if you use GenericQueue for storing void*
pointers instead, make its ctor, copy ctor, and assig operator
private, you'll prevent clients from making instances of it AND you'll
have only one version of the code. And the templated interface class
will implement its functionality in-terms-of GenericQueue
functionality and provide for type safety. The code in the interface
class is substantially less than that of the GenericQueue and will not
result in as much code bloat. No?

Of course I'll have to account for the fact that a pointer to an
object might have been pushed onto multiple stacks.
The idea is from Scott Myer's "Effective C++", Item 42 - Use Private
Inheritance Judiciously.
Jun 27 '08 #9
<os******@gmail .comwrote in message
news:39******** *************** ***********@i36 g2000prf.google groups.com...
: I am implementing a lockless queue, which I don't think the
: std::queue<T*pr ovides. To avoid template-induced code bloat, I am
: implementing a generic the queue using void* pointers. But to gain
: type-safety, I am implementing a templated interface-class.

This is a common optimization technique indeed (used back in early
C++ times). But keep in mind that, nowadays, some compilers/linkers
are able to automatically eliminate redundant copies of the same
code, such as those resulting from multiple template instantiations.

: Now what is confusing me is that this function works
:
:
: SpecialQueue::P ush(T* ptr) { GenericQueue::P ush(ptr); }
:
: and it converts T* to void* no problem.

Indeed: the compiler passes a *copy* of ptr to the generic function.
During this copy, it can make an implicit conversion from T* to void*.

: But when I do
:
: SpecialQueue::P op(T*& refPtr) { GenericQueue::P op(refPtr); }
....
: It gives me this error:
:
: "cannot convert parameter 1 from 'int *' to 'void *&'"

Mind the fact that the int* and void* may have different
in-memory representations (even though this is not the case
on the common processor architectures that you and I use).
You can force the behavior you are looking for by using an
explicit cast:
SpecialQueue::P op(T*& refPtr)
{ GenericQueue::P op(reinterpret_ cast<void*&>(re fPtr)); }
However, this will result in undefined behavior, even though
problems will only happen in "exotic" platforms. It will work
perfectly fine on most architectures, but is not portable.

In proper and portable C++, you would need to write:
bool SpecialQueue::P op(T*& refPtr)
{
void * ptr; // I assume this is an output-only parameter
bool const result = GenericQueue::P op(&ptr);
refPtr = static_cast<T*> (ptr); // ok, works if the
// previously pushed pointer was indeed a T* or NULL
// ( any T* can be converted to void* and then back )
return result;
}
As you see, C++ only allows the implicit conversions that are safe.
(except for those (many) that were inherited from C...)

Cheers -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Jun 27 '08 #10

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

Similar topics

12
6558
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the courses to pass the correct option value and then be displayed at the following URL: http://www.dslextreme.com/users/kevinlyons/selectResults.html I am passing countries, products, and courses. The first two display
58
10181
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
25
2945
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default construction and then.. // some other processing and/or changing 'retval' return retval; }
2
4315
by: Morgan | last post by:
Thanks to all of you because I solved the problem related with my previous post. I simply made confusion with pointers to pointers and then succeeded passing the reference to the first element pointer. :-)) You were right about by mixed code: quickly writing an example to clarify the matter I made a C++ program, sorry for the inconvenience. Anyway, although it all works fine with monodimensional arrays, I still have a problem with...
9
2299
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then receiving it, would that change the answer to the above?
8
2118
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects, each requiring a call to that method to fulfill their purpose. There could be 200, there could be more than 1000. That is a lot of references passed around. It feels heavy. Let us say i changed the signature of the interface method to:
7
2619
by: Wade Wegner | last post by:
Hello, I have been desperately trying to programmatically authenticate a windows user, create their credentials, and then redirect them to a different server while passing the credentials at the same time so that they don't have to login again. Specifically, I have two webservers in the same domain. When I have a user go to Webserver A (which uses basic authentication) I programmatically create either a user credential or impersonate...
22
25604
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to compile. <WebMethod()> _ Public Function VerifySku(ByVal skus As XmlDataDocument) As DataSet Test program : Dim cartSet As DataSet cartSet = ws.VerifySku(cartSet)
11
8128
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
4
5934
by: John Sheppard | last post by:
Hello there I was wondering if anyone could help me, I am trying to pass a typed dataset to a dialoged child form by reference. I have binding sources sitting on the child form. So to refresh them I just set their datasource. I am guessing this is probably what is causing the problem. Is there a better way to do this? Anyway this all works happily and things show up when the record already exists but I have 2 problems ; 1) When I add...
0
9424
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
10223
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
10051
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
10000
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
7413
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
6675
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
5310
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.