473,537 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Transparent Class Wrapper

Ok,
IO want to write a class to wrap around another class, to print to the
screen when the vale is changed. So far I have:
template<class T>
class Tracer
{
T data;
public:

Tracer()
{
cout << "New Tracer:" << T() <<"\n";
data = T();
}

Tracer(const T& t)
{
cout << "New Tracer:" << t<<"\n";
data = t;
}

T& operator=(const T& t)
{
cout << "TracerChanged: " << t<<"\n";
data = t;
}


};
allowing me to write:
Tracer<int> a;
Tracer<int> b = 6;
b = 12;
a=4;

but i can't go the other way around:
int e = a; //Error

Now i seem to remeber an explicit operator, and I googled for it this
morning but no luck??
Any thoughts would be apprieciated!

Mike

Nov 23 '05 #1
4 2330

"mike" <sl***********@hotmail.com> schrieb im Newsbeitrag
news:11**********************@f14g2000cwb.googlegr oups.com...
Ok,
IO want to write a class to wrap around another class, to print to
the
screen when the vale is changed. So far I have:
template<class T>
class Tracer
{
T data;
public:

Tracer()
{
cout << "New Tracer:" << T() <<"\n";
data = T();
}

Tracer(const T& t)
{
cout << "New Tracer:" << t<<"\n";
data = t;
}

T& operator=(const T& t)
{
cout << "TracerChanged: " << t<<"\n";
data = t;
}


};
allowing me to write:
Tracer<int> a;
Tracer<int> b = 6;
b = 12;
a=4;

but i can't go the other way around:
int e = a; //Error

Now i seem to remeber an explicit operator, and I googled for it
this
morning but no luck??
Any thoughts would be apprieciated!

operator T() {return data;}

HTH,
-Gernot


Nov 23 '05 #2
"mike" <sl***********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Ok,
IO want to write a class to wrap around another class, to print to the
screen when the vale is changed. So far I have:


#include <iostream>
using std::cout;
template<class T>
class Tracer
{
T data;
public:

Tracer()
{
cout << "New Tracer:" << T() <<"\n";
data = T();
}
Tracer() : data(T())
{
cout << "New Tracer:" << data << '\n';
}

Tracer(const T& t)
{
cout << "New Tracer:" << t<<"\n";
data = t;
}
Tracer(const T& t) : data(t)
{
cout << "New Tracer:" << t << '\n';
}
T& operator=(const T& t)
{
cout << "TracerChanged: " << t<<"\n";
data = t;
return data = t;
}

operator T() const
{
return data;
}

};
allowing me to write:
int main()
{
Tracer<int> a;
Tracer<int> b = 6;
b = 12;
a=4;

but i can't go the other way around:
int e = a; //Error
return 0;
}

Now i seem to remeber an explicit operator, and I googled for it this
morning but no luck??
Any thoughts would be apprieciated!


What you were looking for is 'conversion operator'.

-Mike
Nov 23 '05 #3
Thanks, thats the ticket!
Mike

Nov 23 '05 #4

"mike" <sl***********@hotmail.com> schrieb im Newsbeitrag
news:11**********************@g49g2000cwa.googlegr oups.com...
Thanks, thats the ticket!
Mike


Ooops! Forgot the "const" at the end:
operator T() const {return data;}

otherwise you might get stange errors some day...
Nov 23 '05 #5

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

Similar topics

2
9578
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A, while an instance of class C should be able to check all methods #defined in C, B and A. #------------------------------------------------
22
2207
by: Ron_Adam | last post by:
Hi, Thanks again for all the helping me understand the details of decorators. I put together a class to create decorators that could make them a lot easier to use. It still has a few glitches in it that needs to be addressed. (1) The test for the 'function' object needs to not test for a string but an object type instead.
2
3525
by: Bart Van der Donck | last post by:
Hi, I'm having a hard time trying to find out why the following lines don't work in Firefox and Netscape. The idea is to change the colour of a table row at onmouseover, and then put it back to the <body>'s original background-image at onmouseout. <html> <head>
1
20163
by: Efkas | last post by:
My application have some level : 1. MyButton class with Label inheritance 2. MyComponent as User Control loading and positionning some of MyButtons 3. MyApp loading and positionning MyComponent I prefer don't insert a background in MyComponent, and using the MyApp one as general background with MyComponent positionned, but I am not able...
8
10586
by: Grahammer | last post by:
Is it possible to set the background of a usercontrol as transparent? I tried setting the background image of the usercontrol to a transparent GIF, but that caused MAJOR problems. I'm making some controls that aren't rectangular and it won't be pretty if I end up with a grey rectangle behind each one. ...
4
9333
by: ray well | last post by:
in my app i need to make a RichTextbox control transparent. i need it to be a like a pane of glass lying on a sheet of paper, where u can see everything on the sheet of paper not covered by text written on the glass pane. i need to be able to see the control or form that is underneath the RichTextbox, and at the same time to be able to write...
3
3952
by: vul | last post by:
I used to use creating headers (label at the top of the screen) for VB6 forms as 2 labels shifted a little bit with different for colors to get a simulation of a shadow. I set BackColor of both labels to Transparent. Everything works fine. I'm trying to use the same approach with VB 2005 windows forms. Although I'm setting labels backcolors...
4
10229
by: Dale | last post by:
I am creating GIF images with transparent backgrounds on-the-fly for a web app and rendering them by using System.Drawing.Image.Save(Response.OutputStream, ImageType.GIF). I am confident that the transparency is working properly because if I save the created image to the local hard disk and then view it in a web page or an image editor, the...
8
10147
by: Brian Ward | last post by:
I am looking for a simple way to set the image transparency in a PictureBox. I have a moving PictureBox containing a graphic image .. moving by incrementing its Left property. The background however shows white as the PictureBox moves but I want it to be transparent. The PictureBox BackColor is set to Transparent .. but no affect. I have...
0
7359
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7296
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
7530
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. ...
0
7680
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...
0
5823
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...
1
5215
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
3342
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...
0
3338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
920
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.