473,545 Members | 666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

more template problem

Hello!

I have some problem with template.

I have two classes called Handle which is a template class and Integer which
is not a template class. The Integer class is just a wrapper class for a
primitive int with some methods. I don't show the Integer class because it
will not add any information to my problem.

Now to my problem in the handle class I have an operator defined as
T* operator&() const
{ return body;
This returns the pointer value for body. But the symbol & is not so
understandable for a pointer.
So why can't I use symbol -> instead of the symbol & in the operator
function like
T* operator->() const
{ return body; }

If I do that I get the following compile error c:\Documents and
Settings\Tony\k au\cplusplus\la b4_c++\start.cp p(19): error C2059: syntax
error : '->'

I meant that using statements like Integer* tt = ->myh2;
will set pointer tt to the same pointer value as body.
On the other hand it might not be so suitable to use the symbol -> because
the syntax seams
strange when there is only one operand.

What symbol is most suitable to use for returning a pointer object from
class Handler.
What I mean is what symbol should be put in instead for ? in the operator
below.
T* operator?() const
{ return body; }
to make it most understandable.

Have you any suggestion ?
//Tony
#include "handle.h"
#include <list>
#include <algorithm>
using namespace std;
typedef Handle<Integer> handle_t;

main()
{
list<handle_t> myList1;
handle_t myh1( new Integer(1) );
handle_t myh2( new Integer(2) );
}

#include "integer.h"
#include <iostream>
using namespace std;

template<class T>
class Handle
{
public:

Handle(T* body_ptr) //Constructor
{ body = body_ptr; }

T* operator&() const
{ return body;

private:
T* body;
};


Jul 23 '05 #1
1 1344
Hi Tony,

Three ways I can think of:

1)

template<class T>
class Handle
{
...
T& operator*()
{ return *body; }
...
};

Handle<T> x;
T* p = &*x;

or
2)

template<class T>
class Handle
{
...
T* operator T*()
{ return body; }
...
};

Handle<T> x;
T* p = x;

or my personal preference
3)

template<class T>
class Handle
{
...
T* get_raw_pointer () // some prefer simply "get"
{ return body; }
...
};

Hope this helps
-Christopher Diggins


Jul 23 '05 #2

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

Similar topics

11
912
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have nice and working allocation deallocation routines. However, I don't want to loose the nice extras of new operator, like - constructor calling,...
1
1456
by: icedac | last post by:
I have some questions about template and c++ itself. ///////////////////////////////////////////////////////////////////////// q1) see follow c++ code, and compile. it's works only IntelC++8.1 but VC71. Do you know why? Which compiler's activity is C++STANDARD? And I wanna be feed back some explain. :)
5
1702
by: Amit | last post by:
Greetings all, I am writing some code somehwat similar to the test code I have below. I am having a variety of issues with template specialization. I am not sure if this is related to something i havent correctly understood related to template specialization or is it some problem related to the compiler. Following is the code..
2
1950
by: Amit | last post by:
Greetings. I am having some problem while using a cast operation(static_cast and/or dynamic_cast) between base and derived objects when passing to functions. what I have is something like this.. template <class T> class Node{ protected: T e; Node* left;
7
2134
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type y = x; but I can't understand the last one which is Array<int>::element_type y = x; We have a typedef T element_type; in the template Array...
11
2567
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them by myself. I know there are already tons of vector and matrix implementations, but I wanted to have one taylored for my needs and without debugging...
19
7898
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a macro: #define min(a,b) ((a<b)?a:b) I thought that another way could be to use a template function: template <class T> T min<T a, T b>
3
3742
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out....
4
1887
by: Alan Woodland | last post by:
I've been trying out more template metaprogramming ideas with typelists (mostly for personal learning, I'm aware boost most probably provides this facility already), and I've run into this small problem here. Comeau online (without C++0x, in strict mode) accepts this example pasted here, (apologies for the length of it). Unfortunately G++...
0
7401
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...
0
7807
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...
1
7419
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...
0
7756
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...
1
5326
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...
0
4944
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...
0
3450
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...
1
1879
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
0
703
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...

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.